home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Rozne / HTTrack 3.40-2 / httrack-3.40-2.exe / {app} / src / htsparse.c < prev    next >
C/C++ Source or Header  |  2006-04-09  |  199KB  |  4,556 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsparse.c parser                                      */
  34. /*       html/javascript/css parser                             */
  35. /*       and other parser routines                              */
  36. /* Author: Xavier Roche                                         */
  37. /* ------------------------------------------------------------ */
  38.  
  39.  
  40. /* Internal engine bytecode */
  41. #define HTS_INTERNAL_BYTECODE
  42.  
  43. #ifndef  _WIN32_WCE
  44. #include <fcntl.h>
  45. #endif
  46. #include <ctype.h>
  47.  
  48. /* File defs */
  49. #include "htscore.h"
  50.  
  51. /* specific definitions */
  52. #include "htsbase.h"
  53. #include "htsnet.h"
  54. #include "htsbauth.h"
  55. #include "htsmd5.h"
  56. #include "htsindex.h"
  57.  
  58. /* external modules */
  59. #include "htsmodules.h"
  60.  
  61. // htswrap_add
  62. #include "htswrap.h"
  63.  
  64. // parser
  65. #include "htsparse.h"
  66. #include "htsback.h"
  67.  
  68. // specific defines
  69. #define urladr   (liens[ptr]->adr)
  70. #define urlfil   (liens[ptr]->fil)
  71. #define savename (liens[ptr]->sav)
  72. #define parenturladr   (liens[liens[ptr]->precedent]->adr)
  73. #define parenturlfil   (liens[liens[ptr]->precedent]->fil)
  74. #define parentsavename (liens[liens[ptr]->precedent]->sav)
  75. #define relativeurladr   ((!parent_relative)?urladr:parenturladr)
  76. #define relativeurlfil   ((!parent_relative)?urlfil:parenturlfil)
  77. #define relativesavename ((!parent_relative)?savename:parentsavename)
  78.  
  79. #define test_flush if (opt->flush) { if (opt->log) { fflush(opt->log); } if (opt->errlog) { fflush(opt->errlog);  } }
  80.  
  81. // does nothing
  82. #define XH_uninit do {} while(0)
  83.  
  84. // version optimisΘe, qui permet de ne pas toucher aux html non modifiΘs (update)
  85. #define REALLOC_SIZE 8192
  86. #define HT_ADD_CHK(A) if (((int) (A)+ht_len+1) >= ht_size) { \
  87.   ht_size=(A)+ht_len+REALLOC_SIZE; \
  88.   ht_buff=(char*) realloct(ht_buff,ht_size); \
  89.   if (ht_buff==NULL) { \
  90.   printf("PANIC! : Not enough memory [%d]\n",__LINE__); \
  91.   XH_uninit; \
  92.   abortLogFmt("not enough memory for current html document in HT_ADD_CHK : realloct(%d) failed" _ ht_size); \
  93.   exit(1); \
  94.   } \
  95. } \
  96.   ht_len+=A;
  97. #define HT_ADD_ADR \
  98.   if ((opt->getmode & 1) && (ptr>0)) { \
  99.   int i=((int) (adr - lastsaved)),j=ht_len; HT_ADD_CHK(i) \
  100.   memcpy(ht_buff+j, lastsaved, i); \
  101.   ht_buff[j+i]='\0'; \
  102.   lastsaved=adr; \
  103.   }
  104. #define HT_ADD(A) \
  105.   if ((opt->getmode & 1) && (ptr>0)) { \
  106.   int i_=strlen(A),j_=ht_len; \
  107.   if (i_) { \
  108.   HT_ADD_CHK(i_) \
  109.   memcpy(ht_buff+j_, A, i_); \
  110.   ht_buff[j_+i_]='\0'; \
  111.   } }
  112. #define HT_ADD_HTMLESCAPED(A) \
  113.   if ((opt->getmode & 1) && (ptr>0)) { \
  114.     int i_, j_; \
  115.     char BIGSTK tempo_[HTS_URLMAXSIZE*2]; \
  116.     escape_for_html_print(A, tempo_); \
  117.     i_=strlen(tempo_); \
  118.     j_=ht_len; \
  119.     if (i_) { \
  120.     HT_ADD_CHK(i_) \
  121.     memcpy(ht_buff+j_, tempo_, i_); \
  122.     ht_buff[j_+i_]='\0'; \
  123.   } }
  124. #define HT_ADD_HTMLESCAPED_FULL(A) \
  125.   if ((opt->getmode & 1) && (ptr>0)) { \
  126.     int i_, j_; \
  127.     char BIGSTK tempo_[HTS_URLMAXSIZE*2]; \
  128.     escape_for_html_print_full(A, tempo_); \
  129.     i_=strlen(tempo_); \
  130.     j_=ht_len; \
  131.     if (i_) { \
  132.     HT_ADD_CHK(i_) \
  133.     memcpy(ht_buff+j_, tempo_, i_); \
  134.     ht_buff[j_+i_]='\0'; \
  135.   } }
  136. #define HT_ADD_START \
  137.   int ht_size=(int)(r->size*5)/4+REALLOC_SIZE; \
  138.   int ht_len=0; \
  139.   char* ht_buff=NULL; \
  140.   if ((opt->getmode & 1) && (ptr>0)) { \
  141.   ht_buff=(char*) malloct(ht_size); \
  142.   if (ht_buff==NULL) { \
  143.   printf("PANIC! : Not enough memory [%d]\n",__LINE__); \
  144.   XH_uninit; \
  145.   abortLogFmt("not enough memory for current html document in HT_ADD_START : malloct(%d) failed" _ ht_size); \
  146.   exit(1); \
  147.   } \
  148.   ht_buff[0]='\0'; \
  149.   }
  150. #define HT_ADD_END { \
  151.   int ok=0;\
  152.   if (ht_buff) { \
  153.     char digest[32+2];\
  154.     INTsys fsize_old=fsize(fconv(savename));\
  155.     digest[0]='\0';\
  156.     domd5mem(ht_buff,ht_len,digest,1);\
  157.     if (fsize_old==ht_len) { \
  158.       int mlen = 0;\
  159.       char* mbuff;\
  160.       cache_readdata(cache,"//[HTML-MD5]//",savename,&mbuff,&mlen);\
  161.       if (mlen) \
  162.         mbuff[mlen]='\0';\
  163.       if ((mlen == 32) && (strcmp(((mbuff!=NULL)?mbuff:""),digest)==0)) {\
  164.         ok=1;\
  165.         if ( (opt->debug>1) && (opt->log!=NULL) ) {\
  166.           fspc(opt->log,"debug"); fprintf(opt->log,"File not re-written (md5): %s"LF,savename);\
  167.           test_flush;\
  168.         }\
  169.       } else {\
  170.         ok=0;\
  171.       } \
  172.     }\
  173.     if (!ok) { \
  174.       file_notify(urladr, urlfil, savename, 1, 1, r->notmodified); \
  175.       fp=filecreate(savename); \
  176.       if (fp) { \
  177.         if (ht_len>0) {\
  178.         if ((INTsys)fwrite(ht_buff,1,ht_len,fp) != ht_len) { \
  179.           int fcheck;\
  180.           if ((fcheck=check_fatal_io_errno())) {\
  181.             opt->state.exit_xh=-1;\
  182.           }\
  183.           if (opt->errlog) {   \
  184.             fspc(opt->errlog,"error"); fprintf(opt->errlog,"Unable to write HTML file %s: %s"LF, savename, strerror(errno));\
  185.             if (fcheck) {\
  186.               fspc(opt->errlog,"error");\
  187.               fprintf(opt->errlog,"* * Fatal write error, giving up"LF);\
  188.             }\
  189.             test_flush;\
  190.           }\
  191.         }\
  192.         }\
  193.         fclose(fp); fp=NULL; \
  194.         if (strnotempty(r->lastmodified)) \
  195.         set_filetime_rfc822(savename,r->lastmodified); \
  196.       } else {\
  197.         int fcheck;\
  198.         if ((fcheck=check_fatal_io_errno())) {\
  199.                   fspc(opt->log,"error"); fprintf(opt->log,"Mirror aborted: disk full or filesystem problems"LF); \
  200.                     test_flush; \
  201.           opt->state.exit_xh=-1;\
  202.         }\
  203.         if (opt->errlog) { \
  204.           fspc(opt->errlog,"error");\
  205.           fprintf(opt->errlog,"Unable to save file %s : %s"LF, savename, strerror(errno));\
  206.           if (fcheck) {\
  207.             fspc(opt->errlog,"error");\
  208.             fprintf(opt->errlog,"* * Fatal write error, giving up"LF);\
  209.           }\
  210.           test_flush;\
  211.         }\
  212.       }\
  213.     } else {\
  214.       file_notify(urladr, urlfil, savename, 0, 0, r->notmodified); \
  215.       filenote(savename,NULL); \
  216.     }\
  217.     if (cache->ndx)\
  218.       cache_writedata(cache->ndx,cache->dat,"//[HTML-MD5]//",savename,digest,(int)strlen(digest));\
  219.   } \
  220.   freet(ht_buff); ht_buff=NULL; \
  221. }
  222. #define HT_ADD_FOP 
  223.  
  224. // COPY IN HTSCORE.C
  225. #define HT_INDEX_END do { \
  226.   if (!makeindex_done) { \
  227.   if (makeindex_fp) { \
  228.   char BIGSTK tempo[1024]; \
  229.   if (makeindex_links == 1) { \
  230.   sprintf(tempo,"<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=%s\">"CRLF,makeindex_firstlink); \
  231.   } else \
  232.   tempo[0]='\0'; \
  233.   fprintf(makeindex_fp,template_footer, \
  234.   "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->", \
  235.   tempo \
  236.   ); \
  237.   fflush(makeindex_fp); \
  238.   fclose(makeindex_fp);  /* α ne pas oublier sinon on passe une nuit blanche */  \
  239.   makeindex_fp=NULL; \
  240.   usercommand(opt,0,NULL,fconcat(opt->path_html,"index.html"),"primary","primary");  \
  241.   } \
  242.   } \
  243.   makeindex_done=1;    /* ok c'est fait */  \
  244. } while(0)
  245.  
  246. // Enregistrement d'un lien:
  247. // on calcule la taille nΘcessaire: taille des 3 chaεnes α stocker (taille forcΘe paire, plus 2 octets de sΘcuritΘ)
  248. // puis on vΘrifie qu'on a assez de marge dans le buffer - sinon on en rΘalloue un autre
  249. // enfin on Θcrit α l'adresse courante du buffer, qu'on incrΘmente. on dΘcrΘmente la taille dispo d'autant ensuite
  250. // codebase: si non nul et si .class stockee on le note pour chemin primaire pour classes
  251. // FA,FS: former_adr et former_fil, lien original
  252. #define liens_record_sav_len(A) 
  253.  
  254. // COPIE DE HTSCORE.C
  255.  
  256. #define liens_record(A,F,S,FA,FF) { \
  257.   int notecode=0; \
  258.   int lienurl_len=((sizeof(lien_url)+HTS_ALIGN-1)/HTS_ALIGN)*HTS_ALIGN,\
  259.   adr_len=strlen(A),\
  260.   fil_len=strlen(F),\
  261.   sav_len=strlen(S),\
  262.   cod_len=0,\
  263.   former_adr_len=strlen(FA),\
  264.   former_fil_len=strlen(FF); \
  265.   if (former_adr_len>0) {\
  266.     former_adr_len=(former_adr_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  267.     former_fil_len=(former_fil_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  268.   } else \
  269.     former_adr_len=former_fil_len=0;\
  270.   if (strlen(F)>6) if (strnotempty(codebase)) if (strfield(F+strlen(F)-6,".class")) {\
  271.     notecode=1; \
  272.     cod_len=strlen(codebase); \
  273.     cod_len=(cod_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  274.   } \
  275.   adr_len=(adr_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  276.   fil_len=(fil_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  277.   sav_len=(sav_len/HTS_ALIGN)*HTS_ALIGN+HTS_ALIGN*2; \
  278.   if ((int) lien_size < (int) (adr_len+fil_len+sav_len+cod_len+former_adr_len+former_fil_len+lienurl_len)) { \
  279.     lien_buffer=(char*) ((void*) calloct(add_tab_alloc,1)); \
  280.     lien_size=add_tab_alloc; \
  281.     if (lien_buffer!=NULL) { \
  282.     liens[lien_tot]=(lien_url*) (void*) lien_buffer; lien_buffer+=lienurl_len; lien_size-=lienurl_len; \
  283.     liens[lien_tot]->firstblock=1; \
  284.     } \
  285.   } else { \
  286.     liens[lien_tot]=(lien_url*) (void*) lien_buffer; lien_buffer+=lienurl_len; lien_size-=lienurl_len; \
  287.     liens[lien_tot]->firstblock=0; \
  288.   } \
  289.   if (liens[lien_tot]!=NULL) { \
  290.     liens[lien_tot]->adr=lien_buffer; lien_buffer+=adr_len; lien_size-=adr_len; \
  291.     liens[lien_tot]->fil=lien_buffer; lien_buffer+=fil_len; lien_size-=fil_len; \
  292.     liens[lien_tot]->sav=lien_buffer; lien_buffer+=sav_len; lien_size-=sav_len; \
  293.     liens[lien_tot]->cod=NULL; \
  294.     if (notecode) { \
  295.       liens[lien_tot]->cod=lien_buffer; \
  296.       lien_buffer+=cod_len; \
  297.       lien_size-=cod_len; \
  298.       strcpybuff(liens[lien_tot]->cod,codebase); \
  299.     } \
  300.     if (former_adr_len>0) {\
  301.       liens[lien_tot]->former_adr=lien_buffer; lien_buffer+=former_adr_len; lien_size-=former_adr_len; \
  302.       liens[lien_tot]->former_fil=lien_buffer; lien_buffer+=former_fil_len; lien_size-=former_fil_len; \
  303.       strcpybuff(liens[lien_tot]->former_adr,FA); \
  304.       strcpybuff(liens[lien_tot]->former_fil,FF); \
  305.     }\
  306.     strcpybuff(liens[lien_tot]->adr,A); \
  307.     strcpybuff(liens[lien_tot]->fil,F); \
  308.     strcpybuff(liens[lien_tot]->sav,S); \
  309.     liens_record_sav_len(liens[lien_tot]); \
  310.     hash_write(hashptr,lien_tot,opt->urlhack);  \
  311.   } \
  312. }
  313.  
  314. #define ENGINE_LOAD_CONTEXT() \
  315.   ENGINE_LOAD_CONTEXT_BASE(); \
  316.   /* */ \
  317.   htsblk* r = stre->r_; \
  318.   hash_struct* hash = stre->hash_; \
  319.   int lien_max = *stre->lien_max_; \
  320.   /* */ \
  321.   int error = * stre->error_; \
  322.   int store_errpage = * stre->store_errpage_; \
  323.   char* codebase = stre->codebase; \
  324.   char* base = stre->base; \
  325.   /* */ \
  326.   int makeindex_done = *stre->makeindex_done_; \
  327.   FILE* makeindex_fp = *stre->makeindex_fp_; \
  328.   int makeindex_links = *stre->makeindex_links_; \
  329.   char* makeindex_firstlink = stre->makeindex_firstlink_; \
  330.   /* */ \
  331.   char *template_header = stre->template_header_; \
  332.   char *template_body = stre->template_body_; \
  333.   char *template_footer = stre->template_footer_; \
  334.   /* */ \
  335.   LLint stat_fragment = *stre->stat_fragment_; \
  336.   TStamp makestat_time = stre->makestat_time; \
  337.   FILE* makestat_fp = stre->makestat_fp
  338.  
  339. #define ENGINE_SAVE_CONTEXT() \
  340.   ENGINE_SAVE_CONTEXT_BASE(); \
  341.   /* */ \
  342.   * stre->error_ = error; \
  343.   * stre->store_errpage_ = store_errpage; \
  344.   * stre->lien_max_ = lien_max; \
  345.   /* */ \
  346.   *stre->makeindex_done_ = makeindex_done; \
  347.   *stre->makeindex_fp_ = makeindex_fp; \
  348.   *stre->makeindex_links_ = makeindex_links; \
  349.   /* */ \
  350.   *stre->stat_fragment_ = stat_fragment
  351.  
  352. #define _FILTERS     (*opt->filters.filters)
  353. #define _FILTERS_PTR (opt->filters.filptr)
  354. #define _ROBOTS      ((robots_wizard*)opt->robotsptr)
  355.  
  356. /* Apply current *adr character for the script automate */
  357. #define AUTOMATE_LOOKUP_CURRENT_ADR() do { \
  358.   if (inscript) { \
  359.   int new_state_pos; \
  360.   new_state_pos=inscript_state[inscript_state_pos][(unsigned char)*adr]; \
  361.   if (new_state_pos < 0) { \
  362.   new_state_pos=inscript_state[inscript_state_pos][INSCRIPT_DEFAULT]; \
  363.   } \
  364.   assertf(new_state_pos >= 0); \
  365.   assertf(new_state_pos*sizeof(inscript_state[0]) < sizeof(inscript_state)); \
  366.   inscript_state_pos=new_state_pos; \
  367.   } \
  368. } while(0)  
  369.  
  370. /* Increment current pointer to 'steps' characters, modifying automate if necessary */
  371. #define INCREMENT_CURRENT_ADR(steps) do { \
  372.   int steps__ = (steps); \
  373.   while(steps__ > 0) { \
  374.   adr++; \
  375.   AUTOMATE_LOOKUP_CURRENT_ADR(); \
  376.   steps__ --; \
  377.   } \
  378. } while(0)
  379.  
  380.  
  381. /* Main parser */
  382. int htsparse(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  383.   /* Load engine variables */
  384.   ENGINE_LOAD_CONTEXT();
  385.  
  386. #if HTS_ANALYSTE
  387.   {
  388.     char* cAddr = r->adr;
  389.     int cSize = (int) r->size;
  390.     if ( (opt->debug>0) && (opt->log!=NULL) ) {
  391.       fspc(opt->log,"info"); fprintf(opt->log,"engine: preprocess-html: %s%s"LF, urladr, urlfil);
  392.     }
  393.     if (hts_htmlcheck_preprocess(&cAddr, &cSize, urladr, urlfil) == 1) {
  394.       r->adr = cAddr;
  395.       r->size = cSize;
  396.     }
  397.   }
  398.   if (hts_htmlcheck(r->adr,(int)r->size,urladr,urlfil)) {
  399. #endif          
  400.     FILE* fp=NULL;      // fichier Θcrit localement 
  401.     char* adr=r->adr;    // pointeur (on parcourt)
  402.     char* lastsaved;    // adresse du dernier octet sauvΘ + 1
  403.     if ( (opt->debug>1) && (opt->log!=NULL) ) {
  404.       fspc(opt->log,"debug"); fprintf(opt->log,"scanning file %s%s (%s).."LF, urladr, urlfil, savename); test_flush;
  405.     }
  406.  
  407.  
  408.     // Indexing!
  409. #if HTS_MAKE_KEYWORD_INDEX
  410.     if (opt->kindex) {
  411.       if (index_keyword(r->adr,r->size,r->contenttype,savename,opt->path_html)) {
  412.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  413.           fspc(opt->log,"debug"); fprintf(opt->log,"indexing file..done"LF); test_flush;
  414.         }
  415.       } else {
  416.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  417.           fspc(opt->log,"debug"); fprintf(opt->log,"indexing file..error!"LF); test_flush;
  418.         }
  419.       }
  420.     }
  421. #endif
  422.  
  423.     // Now, parsing
  424.     if ((opt->getmode & 1) && (ptr>0)) {  // rΘcupΘrer les html sur disque       
  425.       // crΘer le fichier html local
  426.       HT_ADD_FOP;   // Θcrire peu α peu le fichier
  427.     }
  428.  
  429.     if (!error) {
  430.       int detect_title=0;  // dΘtection  du title
  431.       int back_add_stats = opt->state.back_add_stats;
  432.       //
  433.       char* in_media=NULL; // in other media type (real media and so..)
  434.       int intag=0;         // on est dans un tag
  435.       int incomment=0;     // dans un <!--
  436.       int inscript=0;      // dans un scipt pour applets javascript)
  437.       signed char inscript_state[10][257];
  438.       typedef enum { 
  439.         INSCRIPT_START=0,
  440.         INSCRIPT_ANTISLASH,
  441.         INSCRIPT_INQUOTE,
  442.         INSCRIPT_INQUOTE2,
  443.         INSCRIPT_SLASH,
  444.         INSCRIPT_SLASHSLASH,
  445.         INSCRIPT_COMMENT,
  446.         INSCRIPT_COMMENT2,
  447.         INSCRIPT_ANTISLASH_IN_QUOTE,
  448.         INSCRIPT_ANTISLASH_IN_QUOTE2,
  449.         INSCRIPT_DEFAULT=256
  450.       } INSCRIPT;
  451.       INSCRIPT inscript_state_pos=INSCRIPT_START;
  452.       char* inscript_name=NULL; // script tag name
  453.       int inscript_tag=0;  // on est dans un <body onLoad="... terminΘ par >
  454.       char inscript_tag_lastc='\0';
  455.       // terminaison (" ou ') du "<body onLoad=.."
  456.       int inscriptgen=0;     // on est dans un code gΘnΘrant, ex aprΦs obj.write("..
  457.       //int inscript_check_comments=0, inscript_in_comments=0;    // javascript comments
  458.       char scriptgen_q='\0'; // caractΦre faisant office de guillemet (' ou ")
  459.       //int no_esc_utf=0;      // ne pas echapper chars > 127
  460.       int nofollow=0;        // ne pas scanner
  461.       //
  462.       int parseall_lastc='\0';     // dernier caractΦre parsΘ pour parseall
  463.       //int parseall_incomment=0;   // dans un /* */ (exemple: a = /* URL */ "img.gif";)
  464.       //
  465.       char* intag_start = adr;
  466.             char* intag_name = NULL;
  467.       char* intag_startattr=NULL;
  468.       int intag_start_valid=0;
  469.       int intag_ctype=0;
  470.       //
  471.       int   parent_relative=0;    // the parent is the base path (.js, .css..)
  472.       HT_ADD_START;    // dΘbuter
  473.  
  474.       /* Initialize script automate for comments, quotes.. */
  475.       memset(inscript_state, 0xff, sizeof(inscript_state));
  476.       inscript_state[INSCRIPT_START][INSCRIPT_DEFAULT]=INSCRIPT_START;     /* by default, stay in START */
  477.       inscript_state[INSCRIPT_START]['\\']=INSCRIPT_ANTISLASH;             /* #1: \ escapes the next character whatever it is */
  478.       inscript_state[INSCRIPT_ANTISLASH][INSCRIPT_DEFAULT]=INSCRIPT_START;
  479.       inscript_state[INSCRIPT_START]['\'']=INSCRIPT_INQUOTE;               /* #2: ' opens quote and only ' returns to 0 */
  480.       inscript_state[INSCRIPT_INQUOTE][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE;
  481.       inscript_state[INSCRIPT_INQUOTE]['\'']=INSCRIPT_START;
  482.       inscript_state[INSCRIPT_INQUOTE]['\\']=INSCRIPT_ANTISLASH_IN_QUOTE;
  483.       inscript_state[INSCRIPT_START]['\"']=INSCRIPT_INQUOTE2;              /* #3: " opens double-quote and only " returns to 0 */
  484.       inscript_state[INSCRIPT_INQUOTE2][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE2;
  485.       inscript_state[INSCRIPT_INQUOTE2]['\"']=INSCRIPT_START;
  486.       inscript_state[INSCRIPT_INQUOTE2]['\\']=INSCRIPT_ANTISLASH_IN_QUOTE2;
  487.       inscript_state[INSCRIPT_START]['/']=INSCRIPT_SLASH;                  /* #4: / state, default to #0 */
  488.       inscript_state[INSCRIPT_SLASH][INSCRIPT_DEFAULT]=INSCRIPT_START;
  489.       inscript_state[INSCRIPT_SLASH]['/']=INSCRIPT_SLASHSLASH;             /* #5: // with only LF to escape */
  490.       inscript_state[INSCRIPT_SLASHSLASH][INSCRIPT_DEFAULT]=INSCRIPT_SLASHSLASH;
  491.       inscript_state[INSCRIPT_SLASHSLASH]['\n']=INSCRIPT_START;
  492.       inscript_state[INSCRIPT_SLASH]['*']=INSCRIPT_COMMENT;                /* #6: / * with only * / to escape */
  493.       inscript_state[INSCRIPT_COMMENT][INSCRIPT_DEFAULT]=INSCRIPT_COMMENT;
  494.       inscript_state[INSCRIPT_COMMENT]['*']=INSCRIPT_COMMENT2;             /* #7: closing comments */
  495.       inscript_state[INSCRIPT_COMMENT2][INSCRIPT_DEFAULT]=INSCRIPT_COMMENT;
  496.       inscript_state[INSCRIPT_COMMENT2]['/']=INSCRIPT_START;
  497.       inscript_state[INSCRIPT_COMMENT2]['*']=INSCRIPT_COMMENT2;
  498.       inscript_state[INSCRIPT_ANTISLASH_IN_QUOTE][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE;    /* #8: escape in "" */
  499.       inscript_state[INSCRIPT_ANTISLASH_IN_QUOTE2][INSCRIPT_DEFAULT]=INSCRIPT_INQUOTE2;  /* #9: escape in '' */
  500.  
  501.  
  502.       /* statistics */
  503.       if ((opt->getmode & 1) && (ptr>0)) { 
  504.         /*
  505.         HTS_STAT.stat_files++;
  506.         HTS_STAT.stat_bytes+=r->size;
  507.         */
  508.       }
  509.  
  510.       /* Primary list or URLs */
  511.       if (ptr == 0) {
  512.         intag=1;
  513.         intag_start_valid=0;
  514.                 intag_name = NULL;
  515.       }
  516.       /* Check is the file is a .js file */
  517.       else if (
  518.         (compare_mime(r->contenttype, str->url_file, "application/x-javascript")!=0)
  519.         || (compare_mime(r->contenttype, str->url_file, "text/css")!=0)
  520.         ) {      /* JavaScript js file */
  521.           inscript=1;
  522.           if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  523.           inscript_name="script";
  524.           intag=1;     // because aprΦs <script> on y est .. - pas utile
  525.           intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  526.           if ((opt->debug>1) && (opt->log!=NULL)) {
  527.             fspc(opt->log,"debug"); fprintf(opt->log,"note: this file is a javascript file"LF); test_flush;
  528.           }
  529.           // for javascript only
  530.           if (compare_mime(r->contenttype, str->url_file, "application/x-javascript") != 0) {
  531.             // all links must be checked against parent, not this link
  532.             if (liens[ptr]->precedent != 0) {
  533.               parent_relative=1;
  534.             }
  535.           }
  536.         }
  537.         /* Or a real audio */
  538.       else if (compare_mime(r->contenttype, str->url_file, "audio/x-pn-realaudio")!=0) {      /* realaudio link file */
  539.         inscript=intag=0;
  540.         inscript_name="media";
  541.         intag_start_valid=0;
  542.         in_media="LNK";       // real media! -> links
  543.       } 
  544.       /* Or a m3u playlist */
  545.       else if (compare_mime(r->contenttype, str->url_file, "audio/x-mpegurl")!=0) {      /* mp3 link file */
  546.         inscript=intag=0;
  547.         inscript_name="media";
  548.         intag_start_valid=0;
  549.         in_media="LNK";       // m3u! -> links
  550.       } 
  551.       else if (compare_mime(r->contenttype, str->url_file, "application/x-authorware-map")!=0) {      /* macromedia aam file */
  552.         inscript=intag=0;
  553.         inscript_name="media";
  554.         intag_start_valid=0;
  555.         in_media="AAM";       // aam
  556.       } 
  557.  
  558.       // Detect UTF8 format
  559.       //if (is_unicode_utf8((unsigned char*) r->adr, (unsigned int) r->size) == 1) {
  560.       //  no_esc_utf=1;
  561.       //} else {
  562.       //  no_esc_utf=0;
  563.       //}
  564.  
  565.             // Hack to prevent any problems with ram files of other files
  566.       * ( r->adr + r->size ) = '\0';
  567.  
  568.       // ------------------------------------------------------------
  569.       // analyser ce qu'il y a en mΘmoire (fichier html)
  570.       // on scanne les balises
  571.       // ------------------------------------------------------------
  572. #if HTS_ANALYSTE
  573.       _hts_in_html_done=0;     // 0% scannΘs
  574.       _hts_cancel=0;           // pas de cancel
  575.       _hts_in_html_parsing=1;  // flag pour indiquer un parsing
  576. #endif
  577.       base[0]='\0';    // effacer base-href
  578.       lastsaved=adr;
  579.       do {
  580.         int p=0;
  581.         int valid_p=0;      // force to take p even if == 0
  582.         int ending_p='\0';  // ending quote?
  583.         int archivetag_p=0;  // avoid multiple-archives with commas
  584.         int  unquoted_script=0;
  585.         INSCRIPT inscript_state_pos_prev=inscript_state_pos;
  586.         error=0;
  587.  
  588.         /* Hack to avoid NULL char problems with C syntax */
  589.         /* Yes, some bogus HTML pages can embed null chars
  590.         and therefore can not be properly handled if this hack is not done
  591.         */
  592.         if ( ! (*adr) ) {
  593.           if ( ((int) (adr - r->adr)) < r->size)
  594.             *adr=' ';
  595.         }
  596.  
  597.  
  598.  
  599.         /*
  600.         index.html built here
  601.         */
  602.         // Construction index.html (sommaire)
  603.         // Avant de tester les a href,
  604.         // Ici on teste si l'on doit construire l'index vers le(s) site(s) miroir(s)
  605.         if (!makeindex_done) {  // autoriation d'Θcrire un index
  606.           if (!detect_title) {
  607.             if (opt->depth == liens[ptr]->depth) {    // on note toujours les premiers liens
  608.               if (!in_media) {
  609.                 if (opt->makeindex && (ptr>0)) {
  610.                   if (opt->getmode & 1) {  // autorisation d'Θcrire
  611.                     p=strfield(adr,"title");  
  612.                     if (p) {
  613.                       if (*(adr-1)=='/') p=0;    // /title
  614.                     } else {
  615.                       if (strfield(adr,"/html"))
  616.                         p=-1;                    // noter, mais sans titre
  617.                       else if (strfield(adr,"body"))
  618.                         p=-1;                    // noter, mais sans titre
  619.                       else if ( ((int) (adr - r->adr) ) >= (r->size-1) )
  620.                         p=-1;                    // noter, mais sans titre
  621.                       else if ( (int) (adr - r->adr) >= r->size - 2)   // we got to hurry
  622.                         p=-1; // xxc xxc xxc
  623.                     }
  624.                   } else
  625.                     p=0;
  626.  
  627.                   if (p) {    // ok center                            
  628.                     if (makeindex_fp==NULL) {
  629.                       file_notify("", "", fconcat(opt->path_html,"index.html"), 1, 1, 0);
  630.                       verif_backblue(opt,opt->path_html);    // gΘnΘrer gif
  631.                       makeindex_fp=filecreate(fconcat(opt->path_html,"index.html"));
  632.                       if (makeindex_fp!=NULL) {
  633.  
  634.                         // Header
  635.                         fprintf(makeindex_fp,template_header,
  636.                           "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  637.                           );
  638.  
  639.                       } else makeindex_done=-1;    // fait, erreur
  640.                     }
  641.  
  642.                     if (makeindex_fp!=NULL) {
  643.                       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  644.                       char BIGSTK s[HTS_URLMAXSIZE*2];
  645.                       char* a=NULL;
  646.                       char* b=NULL;
  647.                       s[0]='\0';
  648.                       if (p>0) {
  649.                         a=strchr(adr,'>');
  650.                         if (a!=NULL) {
  651.                           a++;
  652.                           while(is_space(*a)) a++;    // sauter espaces & co
  653.                           b=strchr(a,'<');   // prochain tag
  654.                         }
  655.                       }
  656.                       if (lienrelatif(tempo,liens[ptr]->sav,concat(opt->path_html,"index.html"))==0) {
  657.                         detect_title=1;      // ok dΘtectΘ pour cette page!
  658.                         makeindex_links++;   // un de plus
  659.                         strcpybuff(makeindex_firstlink,tempo);
  660.                         //
  661.  
  662.                         /* Hack */
  663.                         if (opt->mimehtml) {
  664.                           strcpybuff(makeindex_firstlink, "cid:primary/primary");
  665.                         }
  666.  
  667.                         if ((b==a) || (a==NULL) || (b==NULL)) {    // pas de titre
  668.                           strcpybuff(s,tempo);
  669.                         } else if ((b-a)<256) {
  670.                           b--;
  671.                           while(is_space(*b)) b--;
  672.                           strncpy(s,a,b-a+1);
  673.                           *(s+(b-a)+1)='\0';
  674.                         }
  675.  
  676.                         // Body
  677.                         fprintf(makeindex_fp,template_body,
  678.                           tempo,
  679.                           s
  680.                           );
  681.  
  682.                       }
  683.                     }
  684.                   }
  685.                 }
  686.               }
  687.  
  688.             } else if (liens[ptr]->depth<opt->depth) {   // on a sautΘ level1+1 et level1
  689.               HT_INDEX_END;
  690.             }
  691.           } // if (opt->makeindex)
  692.         }
  693.         // FIN Construction index.html (sommaire)
  694.         /*
  695.         end -- index.html built here
  696.         */
  697.  
  698.  
  699.  
  700.         /* Parse */
  701.         if (
  702.           (*adr=='<')    /* No starting tag */
  703.           && (!inscript)    /* Not in (java)script */
  704.           && (!incomment)   /* Not in comment (<!--) */
  705.           && (!in_media)    /* Not in media */
  706.           ) { 
  707.             intag=1;
  708.                         intag_ctype=0;
  709.                         //parseall_incomment=0;
  710.                         //inquote=0;  // effacer quote
  711.                         intag_start = adr;
  712.                         for(intag_name = adr + 1 ; is_realspace(*intag_name) ; intag_name++ );
  713.                         intag_start_valid = 1;
  714.                         codebase[0]='\0';    // effacer Θventuel codebase
  715.  
  716.                         /* Meta ? */
  717.                         if (check_tag(intag_start, "meta")) {
  718.                             int pos;
  719.                             // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  720.                             if ((pos = rech_tageq_all(adr, "http-equiv"))) {
  721.                                 const char* token = NULL;
  722.                                 int len = rech_endtoken(adr + pos, &token);
  723.                                 if (len > 0) {
  724.                                     if (strfield(token, "content-type")) {
  725.                                         intag_ctype=1;
  726.                                     }
  727.                                     else if (strfield(token, "refresh")) {
  728.                                         intag_ctype=2;
  729.                                     }
  730.                                 }
  731.                             }
  732.                         }
  733.  
  734.             if (opt->getmode & 1) {  // sauver html
  735.               p=strfield(adr,"</html");
  736.               if (p==0) p=strfield(adr,"<head>");
  737.               // if (p==0) p=strfield(adr,"<doctype");
  738.               if (p) {
  739.                 char* eol="\n";
  740.                 if (strchr(r->adr,'\r'))
  741.                   eol="\r\n";
  742.                 if (strnotempty(opt->footer) || opt->urlmode != 4) {  /* != preserve */
  743.                                     if (strnotempty(opt->footer)) {
  744.                       char BIGSTK tempo[1024+HTS_URLMAXSIZE*2];
  745.                                         char gmttime[256];
  746.                                         tempo[0]='\0';
  747.                                         time_gmt_rfc822(gmttime);
  748.                                         strcatbuff(tempo,eol);
  749.                       sprintf(tempo+strlen(tempo),opt->footer,jump_identification(urladr),urlfil,gmttime,HTTRACK_VERSIONID,"","","","","","","");
  750.                         strcatbuff(tempo,eol);
  751.                                         //fwrite(tempo,1,strlen(tempo),fp);
  752.                                         HT_ADD(tempo);
  753.                                     }
  754.                   if (strnotempty(r->charset)) {
  755.                     HT_ADD("<!-- Added by HTTrack --><meta http-equiv=\"content-type\" content=\"text/html;charset=");
  756.                     HT_ADD(r->charset);
  757.                     HT_ADD("\"><!-- /Added by HTTrack -->");
  758.                     HT_ADD(eol);
  759.                   }
  760.                 }
  761.               }
  762.             }        
  763.  
  764.             // Θliminer les <!-- (commentaires) : intag dΘvalidΘ
  765.             if (*(adr+1)=='!')
  766.               if (*(adr+2)=='-')
  767.                 if (*(adr+3)=='-') {
  768.                   intag=0;
  769.                   incomment=1;
  770.                   intag_start_valid=0;
  771.                 }
  772.  
  773.           }
  774.         else if (
  775.           (*adr=='>')                        /* ending tag */
  776.           && ( (!inscript && !in_media) || (inscript_tag) )  /* and in tag (or in script) */
  777.           ) {
  778.             if (inscript_tag) {
  779.               inscript_tag=inscript=0;
  780.               intag=0;
  781.               incomment=0;
  782.               intag_start_valid=0;
  783.                             intag_name = NULL;
  784.               if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  785.             } else if (!incomment) {
  786.               intag=0; //inquote=0;
  787.  
  788.               // entrΘe dans du javascript?
  789.               // on parse ICI car il se peut qu'on ait eu a parser les src=.. dedans
  790.               //if (!inscript) {  // sinon on est dans un obj.write("..
  791.               if ((intag_start_valid) && 
  792.                 (
  793.                 check_tag(intag_start,"script")
  794.                 ||
  795.                 check_tag(intag_start,"style")
  796.                 )
  797.                 ) {
  798.                   char* a=intag_start;    // <
  799.                   // ** while(is_realspace(*(--a)));
  800.                   if (*a=='<') {  // s√r que c'est un tag?
  801.                     if (check_tag(intag_start,"script"))
  802.                       inscript_name="script";
  803.                     else
  804.                       inscript_name="style";
  805.                     inscript=1;
  806.                     inscript_state_pos=INSCRIPT_START;
  807.                     intag=1;     // because aprΦs <script> on y est .. - pas utile
  808.                     intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  809.                     if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  810.                   }
  811.                 }
  812.             } else {                               /* end of comment? */
  813.               // vΘrifier fermeture correcte
  814.               if ( (*(adr-1)=='-') && (*(adr-2)=='-') ) {
  815.                 intag=0;
  816.                 incomment=0;
  817.                 intag_start_valid=0;
  818.                                 intag_name = NULL;
  819.               }
  820. #if GT_ENDS_COMMENT
  821.               /* wrong comment ending */
  822.               else {
  823.                 /* check if correct ending does not exists
  824.                 <!-- foo > example <!-- bar > is sometimes accepted by browsers
  825.                 when no --> is used somewhere else.. darn those browsers are dirty
  826.                 */
  827.                 if (!strstr(adr,"-->")) {
  828.                   intag=0;
  829.                   incomment=0;
  830.                   intag_start_valid=0;
  831.                                     intag_name = NULL;
  832.                 }
  833.               }
  834. #endif
  835.             }
  836.             //}
  837.           }
  838.           //else if (*adr==34) {
  839.           //  inquote=(inquote?0:1);
  840.           //}
  841.         else if (intag || inscript || in_media) {    // nous sommes dans un tag/commentaire, tester si on recoit un tag
  842.           int p_type=0;
  843.           int p_nocatch=0;
  844.           int p_searchMETAURL=0;  // chercher ..URL=<url>
  845.           int add_class=0;        // ajouter .class
  846.           int add_class_dots_to_patch=0;   // number of '.' in code="x.y.z<realname>"
  847.           char* p_flush=NULL;
  848.  
  849.  
  850.           // ------------------------------------------------------------
  851.           // parsing ΘvolΘ
  852.           // ------------------------------------------------------------
  853.           if (((isalpha((unsigned char)*adr)) || (*adr=='/') || (inscript) || (in_media) || (inscriptgen))) {  // sinon pas la peine de tester..
  854.  
  855.  
  856.             /* caractΦre de terminaison pour "miniparsing" javascript=.. ? 
  857.             (ex: <a href="javascript:()" action="foo"> ) */
  858.             if (inscript_tag) {
  859.               if (inscript_tag_lastc) {
  860.                 if (*adr == inscript_tag_lastc) {
  861.                   /* sortir */
  862.                   inscript_tag=inscript=0;
  863.                   incomment=0;
  864.                   if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  865.                 }
  866.               }
  867.             }
  868.  
  869.             /* automate */
  870.             AUTOMATE_LOOKUP_CURRENT_ADR();
  871.  
  872.  
  873.             // Note:
  874.             // Certaines pages ne respectent pas le html
  875.             // notamment les guillements ne sont pas fixΘs
  876.             // Nous sommes dans un tag, donc on peut faire un test plus
  877.             // large pour pouvoi prendre en compte ces particularitΘs
  878.  
  879.             // α vΘrifier: ACTION, CODEBASE, VRML
  880.  
  881.             if (in_media) {
  882.               if (strcmp(in_media,"LNK")==0) { // real media
  883.                 p=0;
  884.                 valid_p=1;
  885.               }
  886.               else if (strcmp(in_media, "AAM")==0) { // AAM
  887.                 if (is_space((unsigned char)adr[0]) && ! is_space((unsigned char)adr[1])) {
  888.                   char* a = adr + 1;
  889.                   int n = 0;
  890.                   int ok = 0;
  891.                   int dot = 0;
  892.                   while(n < HTS_URLMAXSIZE/2 && a[n] != '\0' &&
  893.                     ( ! is_space((unsigned char)a[n]) || ! ( ok = 1) )
  894.                     ) {
  895.                       if (a[n] == '.') {
  896.                         dot = n;
  897.                       }
  898.                       n++;
  899.                     }
  900.                     if (ok && dot > 0) {
  901.                       char BIGSTK tmp[HTS_URLMAXSIZE/2 + 2];
  902.                       tmp[0] = '\0';
  903.                       strncat(tmp, a + dot + 1, n - dot - 1);
  904.                       if (is_knowntype(tmp) || ishtml_ext(tmp) != -1) {
  905.                         adr++;
  906.                         p = 0;
  907.                         valid_p = 1;
  908.                         unquoted_script = 1;
  909.                       }
  910.                     }
  911.                 }
  912.               }
  913.             } else if (ptr>0) {        /* pas premiΦre page 0 (primary) */
  914.               p=0;  // saut pour le nom de fichier: adresse nom fichier=adr+p
  915.  
  916.               // ------------------------------
  917.               // dΘtection d'Θcriture JavaScript.
  918.               // osons les obj.write et les obj.href=.. ! osons!
  919.               // note: inscript==1 donc on sautera aprΦs les \"
  920.               if (inscript) {
  921.                 if (inscriptgen) {          // on est dΘja dans un objet gΘnΘrant..
  922.                   if (*adr==scriptgen_q) {  // fermeture des " ou '
  923.                     if (*(adr-1)!='\\') {   // non
  924.                       inscriptgen=0;        // ok parsing terminΘ
  925.                     }
  926.                   }
  927.                 } else {
  928.                   char* a=NULL;
  929.                   char check_this_fking_line=0;  // parsing code javascript..
  930.                   char must_be_terminated=0;     // caractΦre obligatoire de terminaison!
  931.                   int token_size;
  932.                   if (!(token_size=strfield(adr,".writeln"))) // dΘtection ...objet.write[ln]("code html")...
  933.                     token_size=strfield(adr,".write");
  934.                   if (token_size) {
  935.                     a=adr+token_size;
  936.                     while(is_realspace(*a)) a++; // sauter espaces
  937.                     if (*a=='(') {  // dΘbut parenthΦse
  938.                       check_this_fking_line=2;  // α parser!
  939.                       must_be_terminated=')';
  940.                       a++;  // sauter (
  941.                     }
  942.                   }
  943.                   // euhh ??? ???
  944.                   /* else if (strfield(adr,".href")) {  // dΘtection ...objet.href="...
  945.                   a=adr+5;
  946.                   while(is_realspace(*a)) a++; // sauter espaces
  947.                   if (*a=='=') {  // ohh un Θgal
  948.                   check_this_fking_line=1;  // α noter!
  949.                   must_be_terminated=';';   // et si t'as oubliΘ le ; tu sais pas coder
  950.                   a++;   // sauter =
  951.                   }
  952.  
  953.                   }*/
  954.  
  955.                   // on a un truc du genre instruction"code gΘnΘrΘ" dont on parse le code
  956.                   if (check_this_fking_line) {
  957.                     while(is_realspace(*a)) a++;
  958.                     if ((*a=='\'') || (*a=='"')) {  // dΘpart de '' ou ""
  959.                       char *b;
  960.                       scriptgen_q=*a;    // quote
  961.                       b=a+1;      // dΘpart de la chaεne
  962.                       // vΘrifier forme ("code") et pas ("code"+var), ingΘrable
  963.                       do {
  964.                         if (*a==scriptgen_q && *(a-1)!='\\')  // quote non slash
  965.                           break;            // sortie
  966.                         else if (*a==10 && *(a-1) != '\\'  /* LF and no continue (\) character */
  967.                           && ( *(a-1) != '\r' || *(a-2) != '\\' ) )  /* and not CRLF and no .. */
  968.                           break;
  969.                         else 
  970.                           a++;  // caractΦre suivant
  971.                       } while((a-b) < HTS_URLMAXSIZE / 2);
  972.                       if (*a==scriptgen_q) {  // fin du quote
  973.                         a++;
  974.                         while(is_realspace(*a)) a++;
  975.                         if (*a==must_be_terminated) {  // parenthΦse fermante: ("..")
  976.  
  977.                           // bon, on doit parser une ligne javascript
  978.                           // 1) si check.. ==1 alors c'est un nom de fichier direct, donc
  979.                           // on fixe p sur le saut nΘcessaire pour atteindre le nom du fichier
  980.                           // et le moteur se dΘbrouillera ensuite tout seul comme un grand
  981.                           // 2) si check==2 c'est un peu plus tordu car lα on gΘnΘre du
  982.                           // code html au sein de code javascript au sein de code html
  983.                           // dans ce cas on doit fixer un flag α un puis ensuite dans la boucle
  984.                           // on devra parser les instructions standard comme <a href etc
  985.                           // NOTE: le code javascript autogΘnΘrΘ n'est pas pris en compte!!
  986.                           // (et ne marche pas dans 50% des cas de toute facon!)
  987.                           if (check_this_fking_line==1) {
  988.                             p=(int) (b - adr);    // calculer saut!
  989.                           } else {
  990.                             inscriptgen=1;        // SCRIPTGEN actif
  991.                             adr=b;                // jump
  992.                           }
  993.  
  994.                           if ((opt->debug>1) && (opt->log!=NULL)) {
  995.                             char str[512];
  996.                             str[0]='\0';
  997.                             strncatbuff(str,b,minimum((int) (a - b + 1), 32));
  998.                             fspc(opt->log,"debug"); fprintf(opt->log,"active code (%s) detected in javascript: %s"LF,(check_this_fking_line==2)?"parse":"pickup",str); test_flush;
  999.                           }
  1000.                         }
  1001.  
  1002.                       }
  1003.  
  1004.                     }
  1005.  
  1006.  
  1007.                   }
  1008.                 }
  1009.               }
  1010.               // fin detection code gΘnΘrant javascript vers html
  1011.               // ------------------------------
  1012.  
  1013.  
  1014.               // analyse proprement dite, A HREF=.. etc..
  1015.               if (!p) {
  1016.                 // si dans un tag, et pas dans un script - sauf si on analyse un obj.write("..
  1017.                 if ((intag && (!inscript)) || inscriptgen) {
  1018.                   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  1019.                     // <A HREF=.. pour les liens HTML
  1020.                     p=rech_tageq(adr,"href");
  1021.                     if (p) {    // href.. tester si c'est une bas href!
  1022.                       if ((intag_start_valid) && check_tag(intag_start, "base")) {  // oui!
  1023.                         // ** note: base href et codebase ne font pas bon mΘnage..
  1024.                         p_type=2;    // c'est un chemin
  1025.                       }
  1026.                     }
  1027.  
  1028.                     /* Tags supplΘmentaires α vΘrifier (<img src=..> etc) */
  1029.                     if (p==0) {
  1030.                       int i=0;
  1031.                       while( (p==0) && (strnotempty(hts_detect[i])) ) {
  1032.                         p=rech_tageq(adr,hts_detect[i]);
  1033.                         if (p) {
  1034.                           /* This is a temporary hack to avoid archive=foo.jar,bar.jar .. */
  1035.                           if (strcmp(hts_detect[i], "archive") == 0) {
  1036.                             archivetag_p = 1;
  1037.                           }
  1038.                         }
  1039.                         i++;
  1040.                       }
  1041.                     }
  1042.  
  1043.                     /* Tags supplΘmentaires en dΘbut α vΘrifier (<object .. hotspot1=..> etc) */
  1044.                     if (p==0) {
  1045.                       int i=0;
  1046.                       while( (p==0) && (strnotempty(hts_detectbeg[i])) ) {
  1047.                         p=rech_tageqbegdigits(adr,hts_detectbeg[i]);
  1048.                         i++;
  1049.                       }
  1050.                     }
  1051.  
  1052.                     /* Tags supplΘmentaires α vΘrifier : URL=.. */
  1053.                     if (p==0) {
  1054.                       int i=0;
  1055.                       while( (p==0) && (strnotempty(hts_detectURL[i])) ) {
  1056.                         p=rech_tageq(adr,hts_detectURL[i]);
  1057.                         i++;
  1058.                       }
  1059.                       if (p) {
  1060.                         if (intag_ctype == 1) {
  1061.                           p = 0;
  1062. #if 0
  1063.                           //if ((pos=rech_tageq(adr, "content"))) {
  1064.                           char temp[256];
  1065.                           char* token = NULL;
  1066.                           int len = rech_endtoken(adr + pos, &token);
  1067.                           if (len > 0 && len < sizeof(temp) - 2) {
  1068.                             char* chpos;
  1069.                             temp[0] = '\0';
  1070.                             strncat(temp, token, len);
  1071.                             if ((chpos = strstr(temp, "charset"))
  1072.                               &&
  1073.                               (chpos = strchr(chpos, '='))
  1074.                               ) {
  1075.                                 chpos++;
  1076.                                 while(is_space(*chpos)) chpod++;
  1077.                                 chpos
  1078.                               }
  1079.                           }
  1080. #endif
  1081.                         }
  1082.                         // <META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.example.com">
  1083.                         else if (intag_ctype == 2) {
  1084.                           p_searchMETAURL=1;
  1085.                         } else {
  1086.                           p = 0;            /* cancel */
  1087.                         }
  1088.                       }
  1089.  
  1090.  
  1091.                     }
  1092.  
  1093.                     /* Tags supplΘmentaires α vΘrifier, mais α ne pas capturer */
  1094.                     if (p==0) {
  1095.                       int i=0;
  1096.                       while( (p==0) && (strnotempty(hts_detectandleave[i])) ) {
  1097.                         p=rech_tageq(adr,hts_detectandleave[i]);
  1098.                         i++;
  1099.                       }
  1100.                       if (p)
  1101.                         p_nocatch=1;      /* ne pas rechercher */
  1102.                     }
  1103.  
  1104.                     /* EvΘnements */
  1105.                     if (p==0 && 
  1106.                       ! inscript          /* we don't want events inside document.write */
  1107.                       ) {
  1108.                         int i=0;
  1109.                         /* dΘtection onLoad etc */
  1110.                         while( (p==0) && (strnotempty(hts_detect_js[i])) ) {
  1111.                           p=rech_tageq(adr,hts_detect_js[i]);
  1112.                           i++;
  1113.                         }
  1114.                         /* non dΘtectΘ - dΘtecter Θgalement les onXxxxx= */
  1115.                         if (p==0) {
  1116.                           if ( (*adr=='o') && (*(adr+1)=='n') && isUpperLetter(*(adr+2)) ) {
  1117.                             p=0;
  1118.                             while(isalpha((unsigned char)adr[p]) && (p<64) ) p++;
  1119.                             if (p<64) {
  1120.                               while(is_space(adr[p])) p++;
  1121.                               if (adr[p]=='=')
  1122.                                 p++;
  1123.                               else p=0;
  1124.                             } else p=0;
  1125.                           }
  1126.                         }
  1127.                         /* OK, ΘvΘnement repΘrΘ */
  1128.                         if (p) {
  1129.                           inscript_tag_lastc=*(adr+p);     /* α attendre α la fin */
  1130.                           adr+=p+1;   /* saut */
  1131.                           /*
  1132.                           On est dΘsormais dans du code javascript
  1133.                           */
  1134.                           inscript_name="";
  1135.                           inscript=inscript_tag=1;
  1136.                           inscript_state_pos=INSCRIPT_START;
  1137.                           if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  1138.                         }
  1139.                         p=0;        /* quoi qu'il arrive, ne rien dΘmarrer ici */
  1140.                       }
  1141.  
  1142.                       // <APPLET CODE=.. pour les applet java.. [CODEBASE (chemin..) α faire]
  1143.                       if (p==0) {
  1144.                         p=rech_tageq(adr,"code");
  1145.                         if (p) {
  1146.                           if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  1147.                             p_type=-1;  // juste le nom de fichier+dossier, Θcire avant codebase 
  1148.                             add_class=1;   // ajouter .class au besoin                         
  1149.  
  1150.                             // vΘrifier qu'il n'y a pas de codebase APRES
  1151.                             // sinon on swappe les deux.
  1152.                             // pas trΦs propre mais c'est ce qu'il y a de plus simple α faire!!
  1153.  
  1154.                             {
  1155.                               char *a;
  1156.                               a=adr;
  1157.                               while((*a) && (*a!='>') && (!rech_tageq(a,"codebase"))) a++;
  1158.                               if (rech_tageq(a,"codebase")) {  // banzai! codebase=
  1159.                                 char* b;
  1160.                                 b=strchr(a,'>');
  1161.                                 if (b) {
  1162.                                   if (((int) (b - adr)) < 1000) {    // au total < 1Ko
  1163.                                     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1164.                                     tempo[0]='\0';
  1165.                                     strncatbuff(tempo,a,(int) (b - a) );
  1166.                                     strcatbuff( tempo," ");
  1167.                                     strncatbuff(tempo,adr,(int) (a - adr - 1));
  1168.                                     // Θventuellement remplire par des espaces pour avoir juste la taille
  1169.                                     while((int) strlen(tempo)<((int) (b - adr)))
  1170.                                       strcatbuff(tempo," ");
  1171.                                     // pas d'erreur?
  1172.                                     if ((int) strlen(tempo) == ((int) (b - adr) )) {
  1173.                                       strncpy(adr,tempo,strlen(tempo));   // PAS d'octet nul α la fin!
  1174.                                       p=0;    // DEVALIDER!!
  1175.                                       p_type=0;
  1176.                                       add_class=0;
  1177.                                     }
  1178.                                   }
  1179.                                 }
  1180.                               }
  1181.                             }
  1182.  
  1183.                           }
  1184.                         }
  1185.                       }
  1186.  
  1187.                       // liens α patcher mais pas α charger (ex: codebase)
  1188.                       if (p==0) {  // note: si non chargΘ (ex: ignorer .class) patchΘ tout de mΩme
  1189.                         p=rech_tageq(adr,"codebase");
  1190.                         if (p) {
  1191.                           if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  1192.                             p_type=-2;
  1193.                           } else p=-1;   // ne plus chercher
  1194.                         }
  1195.                       }
  1196.  
  1197.  
  1198.                       // Meta tags pour robots
  1199.                       if (p==0) {
  1200.                         if (opt->robots) {
  1201.                           if ((intag_start_valid) && check_tag(intag_start,"meta")) {
  1202.                             if (rech_tageq(adr,"name")) {    // name=robots.txt
  1203.                               char tempo[1100];
  1204.                               char* a;
  1205.                               tempo[0]='\0';
  1206.                               a=strchr(adr,'>');
  1207. #if DEBUG_ROBOTS
  1208.                               printf("robots.txt meta tag detected\n");
  1209. #endif
  1210.                               if (a) {
  1211.                                 if (((int) (a - adr)) < 999 ) {
  1212.                                   strncatbuff(tempo,adr,(int) (a - adr));
  1213.                                   if (strstrcase(tempo,"content")) {
  1214.                                     if (strstrcase(tempo,"robots")) {
  1215.                                       if (strstrcase(tempo,"nofollow")) {
  1216. #if DEBUG_ROBOTS
  1217.                                         printf("robots.txt meta tag: nofollow in %s%s\n",urladr,urlfil);
  1218. #endif
  1219.                                         nofollow=1;       // NE PLUS suivre liens dans cette page
  1220.                                         if (opt->errlog) {
  1221.                                           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Link %s%s not scanned (follow robots meta tag)"LF,urladr,urlfil);
  1222.                                           test_flush;
  1223.                                         }
  1224.                                       }
  1225.                                     }
  1226.                                   }
  1227.                                 }
  1228.                               }
  1229.                             }
  1230.                           }
  1231.                         }
  1232.                       }
  1233.  
  1234.                                             // entrΘe dans une applet javascript
  1235.                       /*if (!inscript) {  // sinon on est dans un obj.write("..
  1236.                       if (p==0)
  1237.                       if (rech_sampletag(adr,"script"))
  1238.                       if (check_tag(intag_start,"script")) {
  1239.                       inscript=1;
  1240.                       }
  1241.                       }*/
  1242.  
  1243.                       // Ici on procΦde α une analyse du code javascript pour tenter de rΘcupΘrer
  1244.                       // certains fichiers Θvidents.
  1245.                       // C'est devenu obligatoire vu le nombre de pages qui intΦgrent
  1246.                       // des images rΘactives par exemple
  1247.                   }
  1248.                 } else if (inscript) {
  1249.  
  1250. #if 0
  1251.                   /* Check // javascript comments */
  1252.                   if (*adr == 10 || *adr == 13) {
  1253.                     inscript_check_comments = 1;
  1254.                     inscript_in_comments = 0;
  1255.                   }
  1256.                   else if (inscript_check_comments) {
  1257.                     if (!is_realspace(*adr)) {
  1258.                       inscript_check_comments = 0;
  1259.                       if (adr[0] == '/' && adr[1] == '/') {
  1260.                         inscript_in_comments = 1;
  1261.                       }
  1262.                     }
  1263.                   }
  1264. #endif
  1265.  
  1266.                   /* Parse */
  1267.                   assertf(inscript_name != NULL);
  1268.                   if (
  1269.                     *adr == '/' &&
  1270.                     (
  1271.                     (strfield(adr,"/script") && strfield(inscript_name, "script"))
  1272.                     ||
  1273.                     (strfield(adr,"/style")  && strfield(inscript_name, "style"))
  1274.                     )
  1275.                     ) {
  1276.                       char* a=adr;
  1277.                       //while(is_realspace(*(--a)));
  1278.                       while( is_realspace(*a) ) a--;
  1279.                       a--;
  1280.                       if (*a=='<') {  // s√r que c'est un tag?
  1281.                         inscript=0;
  1282.                         if (opt->parsedebug) { HT_ADD("<@@ /inscript @@>"); }
  1283.                       }
  1284.                     } else if (inscript_state_pos == INSCRIPT_START /*!inscript_in_comments*/) {
  1285.                       /*
  1286.                       Script Analyzing - different types supported:
  1287.                       foo="url"
  1288.                       foo("url") or foo(url)
  1289.                       foo "url"
  1290.                       */
  1291.                       int nc;
  1292.                       char  expected     = '=';          // caractΦre attendu aprΦs
  1293.                       char* expected_end = ";";
  1294.                       int can_avoid_quotes=0;
  1295.                       char quotes_replacement='\0';
  1296.                       int ensure_not_mime=0;
  1297.                       if (inscript_tag)
  1298.                         expected_end=";\"\'";            // voir a href="javascript:doc.location='foo'"
  1299.                       nc = strfield(adr,".src");  // nom.src="image";
  1300.                       if (!nc) nc = strfield(adr,".location");  // document.location="doc"
  1301.                       if (!nc) nc = strfield(adr,":location");  // javascript:location="doc"
  1302.                                             if (!nc) { // location="doc"
  1303.                                                 if ( ( nc = strfield(adr,"location") ) 
  1304.                                                     && !isspace(*(adr - 1))
  1305.                                                     )
  1306.                                                     nc = 0;
  1307.                                             }
  1308.                       if (!nc) nc = strfield(adr,".href");  // document.location="doc"
  1309.                       if (!nc) if ( (nc = strfield(adr,".open")) ) { // window.open("doc",..
  1310.                         expected='(';    // parenthΦse
  1311.                         expected_end="),";  // fin: virgule ou parenthΦse
  1312.                         ensure_not_mime=1;  //* ensure the url is not a mime type */
  1313.                       }
  1314.                       if (!nc) if ( (nc = strfield(adr,".replace")) ) { // window.replace("url")
  1315.                         expected='(';    // parenthΦse
  1316.                         expected_end=")";  // fin: parenthΦse
  1317.                       }
  1318.                       if (!nc) if ( (nc = strfield(adr,".link")) ) { // window.link("url")
  1319.                         expected='(';    // parenthΦse
  1320.                         expected_end=")";  // fin: parenthΦse
  1321.                       }
  1322.                       if (!nc) if ( (nc = strfield(adr,"url")) && (!isalnum(*(adr - 1))) 
  1323.                         && *(adr - 1) != '_'
  1324.                         ) { // url(url)
  1325.                           expected='(';    // parenthΦse
  1326.                           expected_end=")";  // fin: parenthΦse
  1327.                           can_avoid_quotes=1;
  1328.                           quotes_replacement=')';
  1329.                         }
  1330.                         if (!nc) if ( (nc = strfield(adr,"import")) ) { // import "url"
  1331.                           if (is_space(*(adr+nc))) {
  1332.                             expected=0;    // no char expected
  1333.                           } else
  1334.                             nc=0;
  1335.                         }
  1336.                         if (nc) {
  1337.                           char *a;
  1338.                           a=adr+nc;
  1339.                           while(is_realspace(*a)) a++;
  1340.                           if ((*a == expected) || (!expected)) {
  1341.                             if (expected)
  1342.                               a++;
  1343.                             while(is_realspace(*a)) a++;
  1344.                             if ((*a==34) || (*a=='\'') || (can_avoid_quotes)) {
  1345.                               char *b,*c;
  1346.                               int ndelim=1;
  1347.                               if ((*a==34) || (*a=='\''))
  1348.                                 a++;
  1349.                               else
  1350.                                 ndelim=0;
  1351.                               b=a;
  1352.                               if (ndelim) {
  1353.                                 while((*b!=34) && (*b!='\'') && (*b!='\0')) b++;
  1354.                               }
  1355.                               else {
  1356.                                 while((*b != quotes_replacement) && (*b!='\0')) b++;
  1357.                               }
  1358.                               c=b--; c+=ndelim;
  1359.                               while(*c==' ') c++;
  1360.                               if ((strchr(expected_end,*c)) || (*c=='\n') || (*c=='\r')) {
  1361.                                 c-=(ndelim+1);
  1362.                                 if ((int) (c - a + 1)) {
  1363.                                   if (ensure_not_mime) {
  1364.                                     int i = 0;
  1365.                                     while(a != NULL && hts_main_mime[i] != NULL && hts_main_mime[i][0] != '\0') {
  1366.                                       int p;
  1367.                                       if ((p=strfield(a, hts_main_mime[i])) && a[p] == '/') {
  1368.                                         a=NULL;
  1369.                                       }
  1370.                                       i++;
  1371.                                     }
  1372.                                   }
  1373.                                   if (a != NULL) {
  1374.                                     if ((opt->debug>1) && (opt->log!=NULL)) {
  1375.                                       char str[512];
  1376.                                       str[0]='\0';
  1377.                                       strncatbuff(str,a,minimum((int) (c - a + 1),32));
  1378.                                       fspc(opt->log,"debug"); fprintf(opt->log,"link detected in javascript: %s"LF,str); test_flush;
  1379.                                     }
  1380.                                     p=(int) (a - adr);    // p non nul: TRAITER CHAINE COMME FICHIER
  1381.                                     if (can_avoid_quotes) {
  1382.                                       ending_p=quotes_replacement;
  1383.                                     }
  1384.                                   }
  1385.                                 }
  1386.                               }
  1387.  
  1388.  
  1389.                             }
  1390.                           }
  1391.                         }
  1392.  
  1393.                     }
  1394.                 }
  1395.               }
  1396.  
  1397.             } else {      // ptr == 0
  1398.               //p=rech_tageq(adr,"primary");    // lien primaire, yeah
  1399.               p=0;          // No stupid tag anymore, raw link
  1400.               valid_p=1;    // Valid even if p==0
  1401.               while ((adr[p] == '\r') || (adr[p] == '\n'))
  1402.                 p++;
  1403.               //can_avoid_quotes=1;
  1404.               ending_p='\r';
  1405.             }       
  1406.  
  1407.           } else if (isspace((unsigned char)*adr)) {
  1408.             intag_startattr=adr+1;        // attribute in tag (for dirty parsing)
  1409.           }
  1410.  
  1411.  
  1412.           // ------------------------------------------------------------
  1413.           // dernier recours - parsing "sale" : dΘtection systΘmatique des .gif, etc.
  1414.           // risque: gΘnΘrer de faux fichiers parazites
  1415.           // fix: ne parse plus dans les commentaires
  1416.           // ------------------------------------------------------------
  1417.           if ( (opt->parseall) && (ptr>0) && (!in_media) /* && (!inscript_in_comments)*/ ) {   // option parsing "brut"
  1418.             //int incomment_justquit=0;
  1419.             if (!is_realspace(*adr)) {
  1420.               int noparse=0;
  1421.  
  1422.               // Gestion des /* */
  1423. #if 0
  1424.               if (inscript) {
  1425.                 if (parseall_incomment) {
  1426.                   if ((*adr=='/') && (*(adr-1)=='*'))
  1427.                     parseall_incomment=0;
  1428.                   incomment_justquit=1;       // ne pas noter dernier caractΦre
  1429.                 } else {
  1430.                   if ((*adr=='/') && (*(adr+1)=='*'))
  1431.                     parseall_incomment=1;
  1432.                 }
  1433.               } else
  1434.                 parseall_incomment=0;
  1435. #endif
  1436.               /* ensure automate state  0 (not in comments, quotes..) */
  1437.               if (inscript && ( 
  1438.                 inscript_state_pos != INSCRIPT_INQUOTE && inscript_state_pos != INSCRIPT_INQUOTE2
  1439.                 ) ) {
  1440.                   noparse=1;
  1441.                 }
  1442.  
  1443.                 /* vΘrifier que l'on est pas dans un <!-- --> pur */
  1444.                 if ( (!intag) && (incomment) && (!inscript))
  1445.                   noparse=1;        /* commentaire */
  1446.  
  1447.                 // recherche d'URLs
  1448.                 if (!noparse) {
  1449.                   //if ((!parseall_incomment) && (!noparse)) {
  1450.                   if (!p) {                   // non dΘja trouvΘ
  1451.                     if (adr != r->adr) {     // >1 caractΦre
  1452.                       // scanner les chaines
  1453.                       if ((*adr == '\"') || (*adr=='\'')) {         // "xx.gif" 'xx.gif'
  1454.                         if (strchr("=(,",parseall_lastc)) {    // exemple: a="img.gif.. (handles comments)
  1455.                           char *a=adr;
  1456.                           char stop=*adr;  // " ou '
  1457.                           int count=0;
  1458.  
  1459.                           // sauter caractΦres
  1460.                           a++;
  1461.                           // copier
  1462.                           while((*a) && (*a!='\'') && (*a!='\"') && (count<HTS_URLMAXSIZE)) { count++; a++; }
  1463.  
  1464.                           // ok chaine terminΘe par " ou '
  1465.                           if ((*a == stop) && (count<HTS_URLMAXSIZE) && (count>0)) {
  1466.                             char c;
  1467.                             char* aend;
  1468.                             //
  1469.                             aend=a;     // sauver dΘbut
  1470.                             a++;
  1471.                             while(is_taborspace(*a)) a++;
  1472.                             c=*a;
  1473.                             if (strchr("),;>/+\r\n",c)) {     // exemple: ..img.gif";
  1474.                               // le / est pour funct("img.gif" /* URL */);
  1475.                               char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1476.                               char type[256];
  1477.                               int url_ok=0;      // url valide?
  1478.                               tempo[0]='\0'; type[0]='\0';
  1479.                               //
  1480.                               strncatbuff(tempo,adr+1,count);
  1481.                               //
  1482.                               if ((!strchr(tempo,' ')) || inscript) {   // espace dedans: mΘfiance! (sauf dans code javascript)
  1483.                                 int invalid_url=0;
  1484.  
  1485.                                 // escape                              
  1486.                                 unescape_amp(tempo);
  1487.  
  1488.                                 // Couper au # ou ? Θventuel
  1489.                                 {
  1490.                                   char* a=strchr(tempo,'#');
  1491.                                   if (a)
  1492.                                     *a='\0';
  1493.                                   a=strchr(tempo,'?');
  1494.                                   if (a)
  1495.                                     *a='\0';
  1496.                                 }
  1497.  
  1498.                                 // vΘrifier qu'il n'y a pas de caractΦres spΘciaux
  1499.                                 if (!strnotempty(tempo))
  1500.                                   invalid_url=1;
  1501.                                 else if (strchr(tempo,'*')
  1502.                                   || strchr(tempo,'<')
  1503.                                   || strchr(tempo,'>')
  1504.                                   || strchr(tempo,',')    /* list of files ? */
  1505.                                   || strchr(tempo,'\"')    /* potential parsing bug */
  1506.                                   || strchr(tempo,'\'')    /* potential parsing bug */
  1507.                                   )
  1508.                                   invalid_url=1;
  1509.                                 else if (tempo[0] == '.' && isalnum(tempo[1]))   // ".gif"
  1510.                                   invalid_url=1;
  1511.  
  1512.                                 /* non invalide? */
  1513.                                 if (!invalid_url) {
  1514.                                   // Un plus α la fin? Alors ne pas prendre sauf si extension ("/toto.html#"+tag)
  1515.                                   if (c!='+') {    // PAS de plus α la fin
  1516. #if 0
  1517.                                     char* a;
  1518. #endif
  1519.                                     // "Comparisons of scheme names MUST be case-insensitive" (RFC2616)                                  
  1520.                                     if (
  1521.                                       (strfield(tempo,"http:")) 
  1522.                                       || (strfield(tempo,"ftp:"))
  1523. #if HTS_USEOPENSSL
  1524.                                       || (
  1525.                                       SSL_is_available &&
  1526.                                       (strfield(tempo,"https:"))
  1527.                                       )
  1528. #endif
  1529. #if HTS_USEMMS
  1530.                                                                             || strfield(tempo,"mms:")
  1531. #endif
  1532.                                       )  // ok pas de problΦme
  1533.                                       url_ok=1;
  1534.                                     else if (tempo[strlen(tempo)-1]=='/') {        // un slash: ok..
  1535.                                       if (inscript)   // sinon si pas javascript, mΘfiance (rΘpertoire style base?)
  1536.                                         url_ok=1;
  1537.                                     } 
  1538. #if 0
  1539.                                     else if ((a=strchr(tempo,'/'))) {        // un slash: ok..
  1540.                                       if (inscript) {    // sinon si pas javascript, mΘfiance (style "text/css")
  1541.                                         if (strchr(a+1,'/'))     // un seul / : abandon (STYLE type='text/css')
  1542.                                           if (!strchr(tempo,' '))  // avoid spaces (too dangerous for comments)
  1543.                                             url_ok=1;
  1544.                                       }
  1545.                                     }
  1546. #endif
  1547.                                   }
  1548.                                   // Prendre si extension reconnue
  1549.                                   if (!url_ok) {
  1550.                                     get_httptype(type,tempo,0);
  1551.                                     if (strnotempty(type))     // type reconnu!
  1552.                                       url_ok=1;
  1553.                                     else if (is_dyntype(get_ext(tempo)))  // reconnu php,cgi,asp..
  1554.                                       url_ok=1;
  1555.                                     // MAIS pas les foobar@aol.com !!
  1556.                                     if (strchr(tempo,'@'))
  1557.                                       url_ok=0;
  1558.                                   }
  1559.                                   //
  1560.                                   // Ok, cela pourrait Ωtre une URL
  1561.                                   if (url_ok) {
  1562.  
  1563.                                     // Check if not fodbidden tag (id,name..)
  1564.                                     if (intag_start_valid) {
  1565.                                       if (intag_start)
  1566.                                         if (intag_startattr)
  1567.                                           if (intag)
  1568.                                             if (!inscript)
  1569.                                               if (!incomment) {
  1570.                                                 int i=0,nop=0;
  1571.                                                 while( (nop==0) && (strnotempty(hts_nodetect[i])) ) {
  1572.                                                   nop=rech_tageq(intag_startattr,hts_nodetect[i]);
  1573.                                                   i++;
  1574.                                                 }
  1575.                                                 // Forbidden tag
  1576.                                                 if (nop) {
  1577.                                                   url_ok=0;
  1578.                                                   if ((opt->debug>1) && (opt->log!=NULL)) {
  1579.                                                     fspc(opt->log,"debug"); fprintf(opt->log,"dirty parsing: bad tag avoided: %s"LF,hts_nodetect[i-1]); test_flush;
  1580.                                                   }
  1581.                                                 }
  1582.                                               }
  1583.                                     }
  1584.  
  1585.  
  1586.                                     // Accepter URL, on la traitera comme une URL normale!!
  1587.                                     if (url_ok) {
  1588.                                       valid_p = 1;
  1589.                                       p = 0;
  1590.                                     }
  1591.  
  1592.                                   }
  1593.                                 }
  1594.                               }
  1595.                             }
  1596.                           }
  1597.                         }
  1598.                       }
  1599.                     }
  1600.                   }  // p == 0               
  1601.  
  1602.                 } // not in comment
  1603.  
  1604.                 // plus dans un commentaire
  1605.                 if ( inscript_state_pos == INSCRIPT_START 
  1606.                   && inscript_state_pos_prev == INSCRIPT_START) {
  1607.                     parseall_lastc=*adr;             // caractΦre avant le prochain
  1608.                   }
  1609.  
  1610.  
  1611.             }  // if realspace
  1612.           }  // if parseall
  1613.  
  1614.  
  1615.           // ------------------------------------------------------------
  1616.           // p!=0 : on a repΘrΘ un Θventuel lien
  1617.           // ------------------------------------------------------------
  1618.           //
  1619.           if ((p>0) || (valid_p)) {    // on a repΘrΘ un lien
  1620.             //int lien_valide=0;
  1621.             char* eadr=NULL;          /* fin de l'URL */
  1622.             char* quote_adr=NULL;     /* adresse du ? dans l'adresse */
  1623.             int ok=1;
  1624.             char quote='\0';
  1625.             int quoteinscript=0;
  1626.             int  noquote=0;
  1627.                         char *tag_attr_start = adr;
  1628.  
  1629.             // si nofollow ou un stop a ΘtΘ dΘclenchΘ, rΘΘcrire tous les liens en externe
  1630.             if ((nofollow) || (opt->state.stop))
  1631.               p_nocatch=1;
  1632.  
  1633.             // Θcrire codebase avant, flusher avant code
  1634.             if ((p_type==-1) || (p_type==-2)) {
  1635.               if ((opt->getmode & 1) && (ptr>0)) {
  1636.                 HT_ADD_ADR;    // refresh
  1637.               }
  1638.               lastsaved=adr;    // dernier Θcrit+1
  1639.             }
  1640.  
  1641.             // sauter espaces
  1642.             // adr+=p;
  1643.             INCREMENT_CURRENT_ADR(p);
  1644.             while( ( is_space(*adr) || (
  1645.               inscriptgen 
  1646.               && adr[0] == '\\' 
  1647.               && is_space(adr[1])
  1648.               )
  1649.               )
  1650.               && quote == '\0'
  1651.               ) {
  1652.                 if (!quote)
  1653.                   if ((*adr=='\"') || (*adr=='\'')) {
  1654.                     quote=*adr;                     // on doit attendre cela α la fin
  1655.                     if (inscriptgen && *(adr - 1) == '\\') {
  1656.                       quoteinscript=1;  /* will wait for \" */
  1657.                     }
  1658.                   }
  1659.                   // puis quitter
  1660.                   // adr++;    // sauter les espaces, "" et cie
  1661.                   INCREMENT_CURRENT_ADR(1);
  1662.               }
  1663.  
  1664.               /* Stop at \n (LF) if primary links or link lists */
  1665.               if (ptr == 0 || (in_media && strcmp(in_media,"LNK")==0))
  1666.                 quote='\n';
  1667.               /* s'arrΩter que ce soit un ' ou un " : pour document.write('<img src="foo'+a); par exemple! */
  1668.               else if (inscript && ! unquoted_script)
  1669.                 noquote=1;
  1670.  
  1671.               // sauter Θventuel \" ou \' javascript
  1672.               if (inscript) {    // on est dans un obj.write("..
  1673.                 if (*adr=='\\') {
  1674.                   if ((*(adr+1)=='\'') || (*(adr+1)=='"')) {  // \" ou \'
  1675.                     // adr+=2;    // sauter
  1676.                     INCREMENT_CURRENT_ADR(2);
  1677.                   }
  1678.                 }
  1679.               }
  1680.  
  1681.               // sauter content="1;URL=http://..
  1682.               if (p_searchMETAURL) {
  1683.                 int l=0;
  1684.                 while(
  1685.                   (adr + l + 4 < r->adr + r->size)
  1686.                   && (!strfield(adr+l,"URL=")) 
  1687.                   && (l<128) ) l++;
  1688.                 if (!strfield(adr+l,"URL="))
  1689.                   ok=-1;
  1690.                 else
  1691.                   adr+=(l+4);
  1692.               }
  1693.  
  1694.               /* Θviter les javascript:document.location=.. : les parser, plut⌠t */
  1695.               if (ok!=-1) {
  1696.                 if (strfield(adr,"javascript:") 
  1697.                   && ! inscript       /* we don't want to parse 'javascript:' inside document.write inside scripts */
  1698.                   ) {
  1699.                     ok=-1;
  1700.                     /*
  1701.                     On est dΘsormais dans du code javascript
  1702.                     */
  1703.                     inscript_name="";
  1704.                     inscript_tag=inscript=1;
  1705.                     inscript_state_pos=INSCRIPT_START;
  1706.                     inscript_tag_lastc=quote;     /* α attendre α la fin */
  1707.                     if (opt->parsedebug) { HT_ADD("<@@ inscript @@>"); }
  1708.                   }
  1709.               }
  1710.  
  1711.               if (p_type==1) {
  1712.                 if (*adr=='#') {
  1713.                   adr++;           // sauter # pour usemap etc
  1714.                 }
  1715.               }
  1716.               eadr=adr;
  1717.  
  1718.               // ne pas flusher aprΦs code si on doit Θcrire le codebase avant!
  1719.               if ((p_type!=-1) && (p_type!=2) && (p_type!=-2)) {
  1720.                 if ((opt->getmode & 1) && (ptr>0)) {
  1721.                   HT_ADD_ADR;    // refresh
  1722.                 }
  1723.                 lastsaved=adr;    // dernier Θcrit+1
  1724.                 // aprΦs on Θcrira soit les donnΘes initiales,
  1725.                 // soir une URL/lien modifiΘ!
  1726.               } else if (p_type==-1) p_flush=adr;    // flusher jusqu'α adr ensuite
  1727.  
  1728.               if (ok!=-1) {    // continuer
  1729.                 // dΘcouper le lien
  1730.                 do {
  1731.                   if ((* (unsigned char*) eadr)<32) {   // caractΦre de contr⌠le (ou \0)
  1732.                     if (!is_space(*eadr))
  1733.                       ok=0; 
  1734.                   }
  1735.                   if ( ( ((int) (eadr - adr)) ) > HTS_URLMAXSIZE)  // ** trop long, >HTS_URLMAXSIZE caractΦres (on prΘvoit HTS_URLMAXSIZE autres pour path)
  1736.                     ok=-1;    // ne pas traiter ce lien
  1737.  
  1738.                   if (ok > 0) {
  1739.                     //if (*eadr!=' ') {  
  1740.                     if (is_space(*eadr)) {   // guillemets,CR, etc
  1741.                       if ( 
  1742.                         ( *eadr == quote && ( !quoteinscript || *(eadr -1) == '\\') )  // end quote
  1743.                         || ( noquote && (*eadr == '\"' || *eadr == '\'') )       // end at any quote
  1744.                         || (!noquote && quote == '\0' && is_realspace(*eadr) )   // unquoted href
  1745.                         )     // si pas d'attente de quote spΘciale ou si quote atteinte
  1746.                         ok=0; 
  1747.                     } else if (ending_p && (*eadr==ending_p))
  1748.                       ok=0;
  1749.                     else {
  1750.                       switch(*eadr) {
  1751.                     case '>': 
  1752.                       if (!quote) {
  1753.                         if (!inscript && !in_media) {
  1754.                           intag=0;    // PLUS dans un tag!
  1755.                           intag_start_valid=0;
  1756.                                                     intag_name = NULL;
  1757.                         }
  1758.                         ok=0;
  1759.                       }
  1760.                       break;
  1761.                       /*case '<':*/ 
  1762.                     case '#': 
  1763.                       if (*(eadr-1) != '&')       // (
  1764.                         ok=0; 
  1765.                       break;
  1766.                       // case '?': non!
  1767.                     case '\\': if (inscript) ok=0; break;     // \" ou \' point d'arrΩt
  1768.                     case '?': quote_adr=adr; break;           // noter position query
  1769.                       }
  1770.                     }
  1771.                     //}
  1772.                   } 
  1773.                   eadr++;
  1774.                 } while(ok==1);
  1775.  
  1776.                 // Empty link detected
  1777.                 if ( (((int) (eadr - adr))) <= 1) {       // link empty
  1778.                   ok=-1;        // No
  1779.                   if (*adr != '#') {        // Not empty+unique #
  1780.                     if ( (((int) (eadr - adr)) == 1)) {       // 1=link empty with delim (end_adr-start_adr)
  1781.                       if (quote) {
  1782.                         if ((opt->getmode & 1) && (ptr>0)) { 
  1783.                           HT_ADD("#");        // We add this for a <href="">
  1784.                         }
  1785.                       }
  1786.                     }
  1787.                   }
  1788.                 }
  1789.  
  1790.                 // This is a dirty and horrible hack to avoid parsing an Adobe GoLive bogus tag
  1791.                 if (strfield(adr, "(Empty Reference!)")) {
  1792.                   ok=-1;        // No
  1793.                 }
  1794.  
  1795.               }
  1796.  
  1797.               if (ok==0) {    // tester un lien
  1798.                 char BIGSTK lien[HTS_URLMAXSIZE*2];
  1799.                 int meme_adresse=0;      // 0 par dΘfaut pour primary
  1800.                 //char *copie_de_adr=adr;
  1801.                 //char* p;
  1802.  
  1803.                 // construire lien (dΘcoupage)
  1804.                 if ( (((int) (eadr -  adr))-1) < HTS_URLMAXSIZE  ) {    // pas trop long?
  1805.                   strncpy(lien,adr,((int) (eadr - adr))-1);
  1806.                   *(lien+  (((int) (eadr -  adr)))-1  )='\0';
  1807.                   //printf("link: %s\n",lien);          
  1808.                   // supprimer les espaces
  1809.                   while((lien[strlen(lien)-1]==' ') && (strnotempty(lien))) lien[strlen(lien)-1]='\0';
  1810.  
  1811.  
  1812.                 } else
  1813.                   lien[0]='\0';    // erreur
  1814.  
  1815.  
  1816.                 // ------------------------------------------------------
  1817.                 // Lien repΘrΘ et extrait
  1818.                 if (strnotempty(lien)>0) {           // construction du lien
  1819.                   char BIGSTK adr[HTS_URLMAXSIZE*2],fil[HTS_URLMAXSIZE*2];          // ATTENTION adr cache le "vrai" adr
  1820.                   int forbidden_url=-1;              // lien non interdit (mais non autorisΘ..)
  1821.                   int just_test_it=0;                // mode de test des liens
  1822.                   int set_prio_to=0;                 // pour capture de page isolΘe
  1823.                   int import_done=0;                 // lien importΘ (ne pas scanner ensuite *α priori*)
  1824.                   //
  1825.                   adr[0]='\0'; fil[0]='\0';
  1826.                   //
  1827.                   // 0: autorisΘ
  1828.                   // 1: interdit (patcher tout de mΩme adresse)
  1829.  
  1830.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  1831.                     fspc(opt->log,"debug"); fprintf(opt->log,"link detected in html (tag): %s"LF,lien); test_flush;
  1832.                   }
  1833.  
  1834.                   // external check
  1835. #if HTS_ANALYSTE
  1836.                   if (!hts_htmlcheck_linkdetected(lien) || !hts_htmlcheck_linkdetected2(lien, intag_start)) {
  1837.                     error=1;    // erreur
  1838.                     if (opt->errlog) {
  1839.                       fspc(opt->errlog,"error"); fprintf(opt->errlog,"Link %s refused by external wrapper"LF,lien);
  1840.                       test_flush;
  1841.                     }
  1842.                   }
  1843. #endif
  1844.  
  1845. #if HTS_STRIP_DOUBLE_SLASH
  1846.                   // supprimer les // en / (sauf pour http://)
  1847.                   if (opt->urlhack) {
  1848.                     char *a,*p,*q;
  1849.                     int done=0;
  1850.                     a=strchr(lien,':');    // http://
  1851.                     if (a) {
  1852.                       a++;
  1853.                       while(*a=='/') a++;    // position aprΦs http://
  1854.                     } else {
  1855.                       a=lien;                // dΘbut
  1856.                       while(*a=='/') a++;    // position aprΦs http://
  1857.                     }
  1858.                     q=strchr(a,'?');     // ne pas traiter aprΦs '?'
  1859.                     if (!q)
  1860.                       q=a+strlen(a)-1;
  1861.                     while(( p=strstr(a,"//")) && (!done) ) {    // remplacer // par /
  1862.                       if ((int) p>(int) q) {   // aprΦs le ? (toto.cgi?param=1//2.3)
  1863.                         done=1;    // stopper
  1864.                       } else {
  1865.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1866.                         tempo[0]='\0';
  1867.                         strncatbuff(tempo,a,(int) p - (int) a);
  1868.                         strcatbuff (tempo,p+1);
  1869.                         strcpybuff(a,tempo);    // recopier
  1870.                       }
  1871.                     }
  1872.                   }
  1873. #endif
  1874.  
  1875.                   // purger espaces de dΘbut et fin, CR,LF rΘsiduels
  1876.                   // (IMG SRC="foo.<\n><\t>gif<\t>")
  1877.                   {
  1878.                     char* a = lien;
  1879.                     int llen;
  1880.  
  1881.                     // strip ending spaces
  1882.                     llen = ( *a != '\0' ) ? strlen(a) : 0;
  1883.                     while(llen > 0 && is_realspace(lien[llen - 1]) ) {
  1884.                       a[--llen]='\0';
  1885.                     } 
  1886.                     //  skip leading ones
  1887.                     while(is_realspace(*a)) a++;
  1888.                     // strip cr, lf, tab inside URL
  1889.                     llen = 0;
  1890.                     while(*a) {
  1891.                       if (*a != '\n' && *a != '\r' && *a != '\t') {
  1892.                         lien[llen++] = *a;
  1893.                       }
  1894.                       a++;
  1895.                     }
  1896.                     lien[llen] = '\0';
  1897.                   }
  1898.  
  1899.                   // commas are forbidden
  1900.                   if (archivetag_p) {
  1901.                     if (strchr(lien, ',')) {
  1902.                       error=1;    // erreur
  1903.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  1904.                         fspc(opt->log,"debug"); fprintf(opt->log,"link rejected (multiple-archive) %s"LF,lien); test_flush;
  1905.                       }
  1906.                     }
  1907.                   }               
  1908.  
  1909.                   /* Unescape/escape %20 and other   */
  1910.                   {
  1911.                     char BIGSTK query[HTS_URLMAXSIZE*2];
  1912.                     char* a=strchr(lien,'?');
  1913.                     if (a) {
  1914.                       strcpybuff(query,a);
  1915.                       *a='\0';
  1916.                     } else
  1917.                       query[0]='\0';
  1918.                     // conversion & -> & et autres joyeusetΘs
  1919.                     unescape_amp(lien);
  1920.                     unescape_amp(query);
  1921.                     // dΘcoder l'inutile (%2E par exemple) et coder espaces
  1922.                     // XXXXXXXXXXXXXXXXX strcpybuff(lien,unescape_http(lien));
  1923.                     //strcpybuff(lien,unescape_http_unharm(lien, (no_esc_utf)?0:1));
  1924.                                         /* Never unescape high-chars (we don't know the encoding!!) */
  1925.                     strcpybuff(lien,unescape_http_unharm(lien, 1));   /* note: '%' is still escaped */
  1926.                     escape_remove_control(lien);
  1927.                     escape_spc_url(lien);
  1928.                     strcatbuff(lien,query);     /* restore */
  1929.                   }
  1930.  
  1931.                   // convertir les Θventuels \ en des / pour Θviter des problΦmes de reconnaissance!
  1932.                   {
  1933.                     char* a;
  1934.                     for(a = jump_identification(lien) ; *a != '\0' && *a != '?' ; a++) {
  1935.                       if (*a == '\\') {
  1936.                         *a = '/';
  1937.                       }
  1938.                     }
  1939.                   }
  1940.  
  1941.                   // supprimer le(s) ./
  1942.                   while ((lien[0]=='.') && (lien[1]=='/')) {
  1943.                     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1944.                     strcpybuff(tempo,lien+2);
  1945.                     strcpybuff(lien,tempo);
  1946.                   }
  1947.                   if (strnotempty(lien)==0)  // sauf si plus de nom de fichier
  1948.                     strcpybuff(lien,"./");
  1949.  
  1950.                   // vΘrifie les /~machin -> /~machin/
  1951.                   // supposition dangereuse?
  1952.                   // OUI!!
  1953. #if HTS_TILDE_SLASH
  1954.                   if (lien[strlen(lien)-1]!='/') {
  1955.                     char *a=lien+strlen(lien)-1;
  1956.                     // Θviter aussi index~1.html
  1957.                     while (((int) a>(int) lien) && (*a!='~') && (*a!='/') && (*a!='.')) a--;
  1958.                     if (*a=='~') {
  1959.                       strcatbuff(lien,"/");    // ajouter slash
  1960.                     }
  1961.                   }
  1962. #endif
  1963.  
  1964.                   // APPLET CODE="mixer.MixerApplet.class" --> APPLET CODE="mixer/MixerApplet.class"
  1965.                   // yes, this is dirty
  1966.                   // but I'm so lazzy..
  1967.                   // and besides the java "code" convention is really a pain in html code
  1968.                   if (p_type==-1) {
  1969.                     char* a=strrchr(lien,'.');
  1970.                     add_class_dots_to_patch=0;
  1971.                     if (a) {
  1972.                       char* b;
  1973.                       do {
  1974.                         b=strchr(lien,'.');
  1975.                         if ((b != a) && (b)) {
  1976.                           add_class_dots_to_patch++;
  1977.                           *b='/';
  1978.                         }
  1979.                       } while((b != a) && (b));
  1980.                     }
  1981.                   }
  1982.  
  1983.                   // Θliminer les Θventuels :80 (port par dΘfaut!)
  1984.                   if (link_has_authority(lien)) {
  1985.                     char * a;
  1986.                     a=strstr(lien,"//");    // "//" authority
  1987.                     if (a)
  1988.                       a+=2;
  1989.                     else
  1990.                       a=lien;
  1991.                     // while((*a) && (*a!='/') && (*a!=':')) a++;
  1992.                     a=jump_toport(a);
  1993.                     if (a) {  // port
  1994.                       int port=0;
  1995.                       int defport=80;
  1996.                       char* b=a+1;
  1997. #if HTS_USEOPENSSL
  1998.                       // FIXME
  1999.                       //if (strfield(adr, "https:")) {
  2000.                       //}
  2001. #endif
  2002.                       while(isdigit((unsigned char)*b)) { port*=10; port+=(int) (*b-'0'); b++; }
  2003.                       if (port==defport) {  // port 80, default - c'est dΘbile
  2004.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2005.                         tempo[0]='\0';
  2006.                         strncatbuff(tempo,lien,(int) (a - lien));
  2007.                         strcatbuff(tempo,a+3);  // sauter :80
  2008.                         strcpybuff(lien,tempo);
  2009.                       }
  2010.                     }
  2011.                   }
  2012.  
  2013.                   // filtrer les parazites (mailto & cie)
  2014.                   /*
  2015.                   if (strfield(lien,"mailto:")) {  // ne pas traiter
  2016.                   error=1;
  2017.                   } else if (strfield(lien,"news:")) {  // ne pas traiter
  2018.                   error=1;
  2019.                   }
  2020.                   */
  2021.  
  2022.                   // vΘrifier que l'on ne doit pas ajouter de .class
  2023.                   if (!error) {
  2024.                     if (add_class) {
  2025.                       char *a = lien+strlen(lien)-1;
  2026.                       while(( a > lien) && (*a!='/') && (*a!='.')) a--;
  2027.                       if (*a != '.')
  2028.                         strcatbuff(lien,".class");    // ajouter .class
  2029.                       else if (!strfield2(a,".class"))
  2030.                         strcatbuff(lien,".class");    // idem
  2031.                     }
  2032.                   }
  2033.  
  2034.                   // si c'est un chemin, alors vΘrifier (toto/toto.html -> http://www/toto/)
  2035.                   if (!error) {
  2036.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2037.                       fspc(opt->log,"debug"); fprintf(opt->log,"position link check %s"LF,lien); test_flush;
  2038.                     }
  2039.  
  2040.                     if ((p_type==2) || (p_type==-2)) {   // code ou codebase                        
  2041.                       // VΘrifier les codebase=applet (au lieu de applet/)
  2042.                       if (p_type==-2) {    // codebase
  2043.                         if (strnotempty(lien)) {
  2044.                           if (fil[strlen(lien)-1]!='/') {  // pas rΘpertoire
  2045.                             strcatbuff(lien,"/");
  2046.                           }
  2047.                         }
  2048.                       }
  2049.  
  2050.                       /* base has always authority */
  2051.                       if (p_type==2 && !link_has_authority(lien)) {
  2052.                         char BIGSTK tmp[HTS_URLMAXSIZE*2];
  2053.                         strcpybuff(tmp, "http://");
  2054.                         strcatbuff(tmp, lien);
  2055.                         strcpybuff(lien, tmp);
  2056.                       }
  2057.  
  2058.                       /* only one ending / (bug on some pages) */
  2059.                       if ((int)strlen(lien)>2) {
  2060.                         int len = (int) strlen(lien);
  2061.                         while(len > 1 && lien[len-1] == '/' && lien[len-2] == '/' )    /* double // (bug) */
  2062.                           lien[--len]='\0';
  2063.                       }
  2064.                       // copier nom host si besoin est
  2065.                       if (!link_has_authority(lien)) {  // pas de http://
  2066.                         char BIGSTK adr2[HTS_URLMAXSIZE*2],fil2[HTS_URLMAXSIZE*2];  // ** euh ident_url_relatif??
  2067.                         if (ident_url_relatif(lien,urladr,urlfil,adr2,fil2)<0) {                        
  2068.                           error=1;
  2069.                         } else {
  2070.                           strcpybuff(lien,"http://");
  2071.                           strcatbuff(lien,adr2);
  2072.                           if (*fil2!='/')
  2073.                             strcatbuff(lien,"/");
  2074.                           strcatbuff(lien,fil2);
  2075.                           {
  2076.                             char* a;
  2077.                             a=lien+strlen(lien)-1;
  2078.                             while((*a) && (*a!='/') && ( a> lien)) a--;
  2079.                             if (*a=='/') {
  2080.                               *(a+1)='\0';
  2081.                             }
  2082.                           }
  2083.                           //char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2084.                           //strcpybuff(tempo,"http://");
  2085.                           //strcatbuff(tempo,urladr);    // host
  2086.                           //if (*lien!='/')
  2087.                           //  strcatbuff(tempo,"/");
  2088.                           //strcatbuff(tempo,lien);
  2089.                           //strcpybuff(lien,tempo);
  2090.                         }
  2091.                       }
  2092.  
  2093.                       if (!error) {  // pas d'erreur?
  2094.                         if (p_type==2) {   // code ET PAS codebase      
  2095.                           char* a=lien+strlen(lien)-1;
  2096.                           char* start_of_filename = jump_identification(lien);
  2097.                           if (start_of_filename != NULL 
  2098.                             && (start_of_filename = strchr(start_of_filename, '/')) != NULL)
  2099.                             start_of_filename++;
  2100.                           if (start_of_filename == NULL)
  2101.                             strcatbuff(lien, "/");
  2102.                           while( (a > lien) && (*a) && (*a!='/')) a--;
  2103.                           if (*a=='/') {     // ok on a repΘrΘ le dernier /
  2104.                             if (start_of_filename != NULL && a >= start_of_filename) {
  2105.                               *(a+1)='\0';   // couper
  2106.                             }
  2107.                           } else {
  2108.                             *lien='\0';    // Θliminer
  2109.                             error=1;   // erreur, ne pas poursuivre
  2110.                           }      
  2111.                         }
  2112.  
  2113.                         // stocker base ou codebase?
  2114.                         switch(p_type) {
  2115.                       case 2: { 
  2116.                         //if (*lien!='/') strcatbuff(base,"/");
  2117.                         strcpybuff(base,lien);
  2118.                               }
  2119.                               break;      // base
  2120.                       case -2: {
  2121.                         //if (*lien!='/') strcatbuff(codebase,"/");
  2122.                         strcpybuff(codebase,lien); 
  2123.                                }
  2124.                                break;  // base
  2125.                         }
  2126.  
  2127.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2128.                           fspc(opt->log,"debug"); fprintf(opt->log,"code/codebase link %s base %s"LF,lien,base); test_flush;
  2129.                         }
  2130.                         //printf("base code: %s - %s\n",lien,base);
  2131.                       }
  2132.  
  2133.                     } else {
  2134.                       char* _base;
  2135.                       if (p_type==-1)   // code (applet)
  2136.                         _base=codebase;
  2137.                       else
  2138.                         _base=base;
  2139.  
  2140.  
  2141.                       // ajouter chemin de base href..
  2142.                       if (strnotempty(_base)) {       // considΘrer base
  2143.                         if (!link_has_authority(lien)) {    // non absolue
  2144.                           if (*lien!='/') {           // non absolu sur le site (/)
  2145.                             if ( ((int) strlen(_base)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  2146.                               // mailto: and co: do NOT add base
  2147.                               if (ident_url_relatif(lien,urladr,urlfil,adr,fil)>=0) {
  2148.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2149.                                 // base est absolue
  2150.                                 strcpybuff(tempo,_base);
  2151.                                 strcatbuff(tempo,lien + ((*lien=='/')?1:0) );
  2152.                                 strcpybuff(lien,tempo);        // patcher en considΘrant base
  2153.                                 // ** vΘrifier que ../ fonctionne (ne doit pas arriver mais bon..)
  2154.  
  2155.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2156.                                   fspc(opt->log,"debug"); fprintf(opt->log,"link modified with code/codebase %s"LF,lien); test_flush;
  2157.                                 }
  2158.                               }
  2159.                             } else {
  2160.                               error=1;    // erreur
  2161.                               if (opt->errlog) {
  2162.                                 fspc(opt->errlog,"error"); fprintf(opt->errlog,"Link %s too long with base href"LF,lien);
  2163.                                 test_flush;
  2164.                               }
  2165.                             }
  2166.                           } else {
  2167.                             char BIGSTK badr[HTS_URLMAXSIZE*2], bfil[HTS_URLMAXSIZE*2];
  2168.                             if (ident_url_absolute(_base, badr, bfil) >=0 ) {
  2169.                               if ( ((int) strlen(badr)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  2170.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2171.                                 // base est absolue
  2172.                                 tempo[0] = '\0';
  2173.                                 if (!link_has_authority(badr)) {
  2174.                                   strcatbuff(tempo, "http://");
  2175.                                 }
  2176.                                 strcatbuff(tempo,badr);
  2177.                                 strcatbuff(tempo,lien);
  2178.                                 strcpybuff(lien,tempo);        // patcher en considΘrant base
  2179.  
  2180.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2181.                                   fspc(opt->log,"debug"); fprintf(opt->log,"link modified with code/codebase %s"LF,lien); test_flush;
  2182.                                 }
  2183.                               } else {
  2184.                                 error=1;    // erreur
  2185.                                 if (opt->errlog) {
  2186.                                   fspc(opt->errlog,"error"); fprintf(opt->errlog,"Link %s too long with base href"LF,lien);
  2187.                                   test_flush;
  2188.                                 }
  2189.                               }
  2190.                             }
  2191.                           }
  2192.                         }
  2193.                       }
  2194.  
  2195.  
  2196.                     }
  2197.                   }
  2198.  
  2199.  
  2200.                   // transformer lien quelconque (http, relatif, etc) en une adresse
  2201.                   // et un chemin+fichier (adr,fil)
  2202.                   if (!error) {
  2203.                     int reponse;
  2204.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2205.                       fspc(opt->log,"debug"); fprintf(opt->log,"build relative link %s with %s%s"LF,lien,relativeurladr,relativeurlfil); test_flush;
  2206.                     }
  2207.                     if ((reponse=ident_url_relatif(lien,relativeurladr,relativeurlfil,adr,fil))<0) {                        
  2208.                       adr[0]='\0';    // erreur
  2209.                       if (reponse==-2) {
  2210.                         if (opt->errlog) {
  2211.                           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Link %s not caught (unknown protocol)"LF,lien);
  2212.                           test_flush;
  2213.                         }
  2214.                       } else {
  2215.                         if ((opt->debug>1) && (opt->errlog!=NULL)) {
  2216.                           fspc(opt->errlog,"debug"); fprintf(opt->errlog,"ident_url_relatif failed for %s with %s%s"LF,lien,relativeurladr,relativeurlfil); test_flush;
  2217.                         }
  2218.                       }
  2219.                     } else {
  2220.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  2221.                         fspc(opt->log,"debug"); fprintf(opt->log,"built relative link %s with %s%s -> %s%s"LF,lien,relativeurladr,relativeurlfil,adr,fil); test_flush;
  2222.                       }
  2223.                     }
  2224.                   } else {
  2225.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2226.                       fspc(opt->log,"debug"); fprintf(opt->log,"link %s not build, error detected before"LF,lien); test_flush;
  2227.                     }
  2228.                     adr[0]='\0';
  2229.                   }
  2230.  
  2231. #if HTS_CHECK_STRANGEDIR
  2232.                   // !ATTENTION!
  2233.                   // Ici on teste les exotiques du genre www.truc.fr/machin (sans slash α la fin)
  2234.                   // je n'ai pas encore trouvΘ le moyen de faire la diffΘrence entre un rΘpertoire
  2235.                   // et un fichier en http A PRIORI : je fais donc un test
  2236.                   // En cas de moved xxx, on recalcule adr et fil, tout simplement
  2237.                   // DEFAUT: test effectuΘ plusieurs fois! α revoir!!!
  2238.                   if ((adr[0]!='\0') && (strcmp(adr,"file://") && (p_type!=2) && (p_type!=-2)) {
  2239.                     //## if ((adr[0]!='\0') && (adr[0]!=lOCAL_CHAR) && (p_type!=2) && (p_type!=-2)) {
  2240.                     if (fil[strlen(fil)-1]!='/') {  // pas rΘpertoire
  2241.                       if (ishtml(fil)==-2) {    // pas d'extension
  2242.                         char BIGSTK loc[HTS_URLMAXSIZE*2];  // Θventuelle nouvelle position
  2243.                         loc[0]='\0';
  2244.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2245.                           fspc(opt->log,"debug"); fprintf(opt->log,"link-check-directory: %s%s"LF,adr,fil);
  2246.                           test_flush;
  2247.                         }
  2248.  
  2249.                         // tester Θventuelle nouvelle position
  2250.                         switch (http_location(adr,fil,loc).statuscode) {
  2251.                       case 200: // ok au final
  2252.                         if (strnotempty(loc)) {  // a changΘ d'adresse
  2253.                           if (opt->errlog) {
  2254.                             fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Link %s%s has moved to %s for %s%s"LF,adr,fil,loc,urladr,urlfil);
  2255.                             test_flush;
  2256.                           }
  2257.  
  2258.                           // recalculer adr et fil!
  2259.                           if (ident_url_absolute(loc,adr,fil)==-1) {
  2260.                             adr[0]='\0';  // cancel
  2261.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  2262.                               fspc(opt->log,"debug"); fprintf(opt->log,"link-check-dir: %s%s"LF,adr,fil);
  2263.                               test_flush;
  2264.                             }
  2265.                           }
  2266.  
  2267.                         }
  2268.                         break;
  2269.                       case -2: case -3:  // timeout ou erreur grave
  2270.                         if (opt->errlog) {
  2271.                           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Connection too slow for testing link %s%s (from %s%s)"LF,adr,fil,urladr,urlfil);
  2272.                           test_flush;
  2273.                         }
  2274.  
  2275.                         break;
  2276.                         }
  2277.  
  2278.                       }
  2279.                     } 
  2280.                   }
  2281. #endif
  2282.  
  2283.                   // Le lien doit juste Ωtre rΘΘcrit, mais ne doit pas gΘnΘrer un lien
  2284.                   // exemple: <FORM ACTION="url_cgi">
  2285.                   if (p_nocatch) {
  2286.                     forbidden_url=1;    // interdire rΘcupΘration du lien
  2287.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  2288.                       fspc(opt->log,"debug"); fprintf(opt->log,"link forced external at %s%s"LF,adr,fil);
  2289.                       test_flush;
  2290.                     }
  2291.                   }
  2292.  
  2293.                   // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  2294.                   // forbidden_url=1 : lien refusΘ
  2295.                   // forbidden_url=0 : lien acceptΘ
  2296.                   //if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2297.                   if ((p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2298.                     if (!p_nocatch) {
  2299.                       if (adr[0]!='\0') {          
  2300.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2301.                           fspc(opt->log,"debug"); fprintf(opt->log,"wizard link test at %s%s.."LF,adr,fil);
  2302.                           test_flush;
  2303.                         }
  2304.                         forbidden_url=hts_acceptlink(opt,ptr,lien_tot,liens,
  2305.                           adr,fil,
  2306.                                                     intag_name ? intag_name : NULL, intag_name ? tag_attr_start : NULL,
  2307.                           &set_prio_to,
  2308.                           &just_test_it);
  2309.                         if ((opt->debug>1) && (opt->log!=NULL)) {
  2310.                           fspc(opt->log,"debug"); fprintf(opt->log,"result for wizard link test: %d"LF,forbidden_url);
  2311.                           test_flush;
  2312.                         }
  2313.                       }
  2314.                     }
  2315.                   }
  2316.  
  2317.                   // calculer meme_adresse
  2318.                   meme_adresse=strfield2(jump_identification(adr),jump_identification(urladr));
  2319.  
  2320.                   // DΘbut partie sauvegarde
  2321.  
  2322.                   // ici on forme le nom du fichier α sauver, et on patche l'URL
  2323.                   if (adr[0]!='\0') {
  2324.                     // savename: simplifier les ../ et autres joyeusetΘs
  2325.                     char BIGSTK save[HTS_URLMAXSIZE*2];
  2326.                     int r_sv=0;
  2327.                     // En cas de moved, adresse premiΦre
  2328.                     char BIGSTK former_adr[HTS_URLMAXSIZE*2];
  2329.                     char BIGSTK former_fil[HTS_URLMAXSIZE*2];
  2330.                     //
  2331.                     save[0]='\0'; former_adr[0]='\0'; former_fil[0]='\0';
  2332.                     //
  2333.  
  2334.                     // nom du chemin α sauver si on doit le calculer
  2335.                     // note: url_savename peut dΘcider de tester le lien si il le trouve
  2336.                     // suspect, et modifier alors adr et fil
  2337.                     // dans ce cas on aura une rΘfΘrence directe au lieu des traditionnels
  2338.                     // moved en cascade (impossible α reproduire α priori en local, lorsque des fichiers
  2339.                     // gif sont impliquΘs par exemple)
  2340.                     if ((p_type!=2) && (p_type!=-2)) {  // pas base href ou codebase
  2341.                       if (forbidden_url!=1) {
  2342.                         char BIGSTK last_adr[HTS_URLMAXSIZE*2];
  2343.                         last_adr[0]='\0';
  2344.                         //char last_fil[HTS_URLMAXSIZE*2]="";
  2345.                         strcpybuff(last_adr,adr);    // ancienne adresse
  2346.                         //strcpybuff(last_fil,fil);    // ancien chemin
  2347.                         r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL);
  2348.                         if (strcmp(jump_identification(last_adr),jump_identification(adr)) != 0) {  // a changΘ
  2349.  
  2350.                           // 2e test si moved
  2351.  
  2352.                           // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  2353.                           // forbidden_url=1 : lien refusΘ
  2354.                           // forbidden_url=0 : lien acceptΘ
  2355.                           if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  2356.                             if (!p_nocatch) {
  2357.                               if (adr[0]!='\0') {          
  2358.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2359.                                   fspc(opt->log,"debug"); fprintf(opt->log,"wizard moved link retest at %s%s.."LF,adr,fil);
  2360.                                   test_flush;
  2361.                                 }
  2362.                                 forbidden_url=hts_acceptlink(opt,ptr,lien_tot,liens,
  2363.                                   adr,fil,
  2364.                                   intag_name ? intag_name : NULL, intag_name ? tag_attr_start : NULL,
  2365.                                   &set_prio_to,
  2366.                                   &just_test_it);
  2367.                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2368.                                   fspc(opt->log,"debug"); fprintf(opt->log,"result for wizard moved link retest: %d"LF,forbidden_url);
  2369.                                   test_flush;
  2370.                                 }
  2371.                               }
  2372.                             }
  2373.                           }
  2374.  
  2375.                           //import_done=1;    // c'est un import!
  2376.                           meme_adresse=0;   // on a changΘ
  2377.                         }
  2378.                       } else {
  2379.                         strcpybuff(save,"");  // dummy
  2380.                       }
  2381.                     }
  2382.  
  2383.                     // resolve unresolved type
  2384.                     if (r_sv!=-1
  2385.                       && p_type != 2 && p_type != -2
  2386.                       && forbidden_url == 0
  2387.                       && IS_DELAYED_EXT(save)
  2388.                       ) 
  2389.                     {  // pas d'erreur, on continue
  2390.                       r_sv = hts_wait_delayed(str, adr, fil, save, former_adr, former_fil, &forbidden_url);
  2391.                     }
  2392.  
  2393.                     // record!
  2394.                     if (r_sv!=-1) {  // pas d'erreur, on continue
  2395.                       /* log */
  2396.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  2397.                         fspc(opt->log,"debug");
  2398.                         if (forbidden_url!=1) {    // le lien va Ωtre chargΘ
  2399.                           if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, pas un lien
  2400.                             fprintf(opt->log,"Code/Codebase: %s%s"LF,adr,fil);
  2401.                           } else if ((opt->getmode & 4)==0) {
  2402.                             fprintf(opt->log,"Record: %s%s -> %s"LF,adr,fil,save);
  2403.                           } else {
  2404.                             if (!ishtml(fil))
  2405.                               fprintf(opt->log,"Record after: %s%s -> %s"LF,adr,fil,save);
  2406.                             else
  2407.                               fprintf(opt->log,"Record: %s%s -> %s"LF,adr,fil,save);
  2408.                           } 
  2409.                         } else
  2410.                           fprintf(opt->log,"External: %s%s"LF,adr,fil);
  2411.                         test_flush;
  2412.                       }
  2413.                       /* FIN log */
  2414.  
  2415.                       // Θcrire lien
  2416.                       if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, sauter
  2417.                         lastsaved=eadr-1+1;  // sauter "
  2418.                       }
  2419.                       /* */
  2420.                       else if (opt->urlmode==0) {    // URL absolue dans tous les cas
  2421.                         if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2422.                           if (!link_has_authority(adr)) {
  2423.                             HT_ADD("http://");
  2424.                           } else {
  2425.                             char* aut = strstr(adr, "//");
  2426.                             if (aut) {
  2427.                               char tmp[256];
  2428.                               tmp[0]='\0';
  2429.                               strncatbuff(tmp, adr, (int) (aut - adr));   // scheme
  2430.                               HT_ADD(tmp);          // Protocol
  2431.                               HT_ADD("//");
  2432.                             }
  2433.                           }
  2434.  
  2435.                           if (!opt->passprivacy) {
  2436.                             HT_ADD_HTMLESCAPED(jump_protocol(adr));           // Password
  2437.                           } else {
  2438.                             HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2439.                           }
  2440.                           if (*fil!='/')
  2441.                             HT_ADD("/");
  2442.                           HT_ADD_HTMLESCAPED(fil);
  2443.                         }
  2444.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2445.                         /* */
  2446.                       } else if (opt->urlmode >= 4) {    // ne rien faire dans tous les cas!
  2447.                         /* */
  2448.                         /* leave the link 'as is' */
  2449.                         /* Sinon, dΘpend de interne/externe */
  2450.                       } else if (forbidden_url==1) {    // le lien ne sera pas chargΘ, rΘfΘrence externe!
  2451.                         if ((opt->getmode & 1) && (ptr>0)) {
  2452.                           if (p_type!=-1) {     // pas que le nom de fichier (pas classe java)
  2453.                             if (!opt->external) {
  2454.                               if (!link_has_authority(adr)) {
  2455.                                 HT_ADD("http://");
  2456.                                 if (!opt->passprivacy) {
  2457.                                   HT_ADD_HTMLESCAPED(adr);     // Password
  2458.                                 } else {
  2459.                                   HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2460.                                 }
  2461.                                 if (*fil!='/')
  2462.                                   HT_ADD("/");
  2463.                                 HT_ADD_HTMLESCAPED(fil);
  2464.                               } else {
  2465.                                 char* aut = strstr(adr, "//");
  2466.                                 if (aut) {
  2467.                                   char tmp[256];
  2468.                                   tmp[0]='\0';
  2469.                                   strncatbuff(tmp, adr, (int) (aut - adr));   // scheme
  2470.                                   HT_ADD(tmp);          // Protocol
  2471.                                   HT_ADD("//");
  2472.                                   if (!opt->passprivacy) {
  2473.                                     HT_ADD_HTMLESCAPED(jump_protocol(adr));          // Password
  2474.                                   } else {
  2475.                                     HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2476.                                   }
  2477.                                   if (*fil!='/')
  2478.                                     HT_ADD("/");
  2479.                                   HT_ADD_HTMLESCAPED(fil);
  2480.                                 }
  2481.                               }
  2482.                               //
  2483.                             } else {    // fichier/page externe, mais on veut gΘnΘrer une erreur
  2484.                               //
  2485.                               int patch_it=0;
  2486.                               int add_url=0;
  2487.                               char* cat_name=NULL;
  2488.                               char* cat_data=NULL;
  2489.                               int cat_nb=0;
  2490.                               int cat_data_len=0;
  2491.  
  2492.                               // ajouter lien external
  2493.                               switch ( (link_has_authority(adr)) ? 1 : ( (fil[strlen(fil)-1]=='/')?1:(ishtml(fil))  ) ) {
  2494.                             case 1: case -2:       // html ou rΘpertoire
  2495.                               if (opt->getmode & 1) {  // sauver html
  2496.                                 patch_it=1;   // redirect
  2497.                                 add_url=1;    // avec link?
  2498.                                 cat_name="external.html";
  2499.                                 cat_nb=0;
  2500.                                 cat_data=HTS_DATA_UNKNOWN_HTML;
  2501.                                 cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  2502.                               }
  2503.                               break;
  2504.                             default:    // inconnu
  2505.                               // asp, cgi..
  2506.                               if ( (strfield2(fil+max(0,(int)strlen(fil)-4),".gif")) 
  2507.                                 || (strfield2(fil+max(0,(int)strlen(fil)-4),".jpg")) 
  2508.                                 || (strfield2(fil+max(0,(int)strlen(fil)-4),".xbm")) 
  2509.                                 /*|| (ishtml(fil)!=0)*/ ) {
  2510.                                 patch_it=1;   // redirect
  2511.                               add_url=1;    // avec link aussi
  2512.                               cat_name="external.gif";
  2513.                               cat_nb=1;
  2514.                               cat_data=HTS_DATA_UNKNOWN_GIF;
  2515.                               cat_data_len=HTS_DATA_UNKNOWN_GIF_LEN;
  2516.                                 } else /* if (is_dyntype(get_ext(fil))) */ {
  2517.                                   patch_it=1;   // redirect
  2518.                                   add_url=1;    // avec link?
  2519.                                   cat_name="external.html";
  2520.                                   cat_nb=0;
  2521.                                   cat_data=HTS_DATA_UNKNOWN_HTML;
  2522.                                   cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  2523.                                 }
  2524.                                 break;
  2525.                               }// html,gif
  2526.  
  2527.                               if (patch_it) {
  2528.                                 char BIGSTK save[HTS_URLMAXSIZE*2];
  2529.                                 char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2530.                                 strcpybuff(save,opt->path_html);
  2531.                                 strcatbuff(save,cat_name);
  2532.                                 if (lienrelatif(tempo,save, relativesavename)==0) {
  2533.                                                                     /* Never escape high-chars (we don't know the encoding!!) */
  2534.                                   escape_uri_utf(tempo);     // escape with %xx
  2535.                                   //if (!no_esc_utf)
  2536.                                   //  escape_uri(tempo);     // escape with %xx
  2537.                                   //else
  2538.                                   //  escape_uri_utf(tempo);     // escape with %xx
  2539.                                   HT_ADD_HTMLESCAPED(tempo);    // page externe
  2540.                                   if (add_url) {
  2541.                                     HT_ADD("?link=");    // page externe
  2542.  
  2543.                                     // same as above
  2544.                                     if (!link_has_authority(adr)) {
  2545.                                       HT_ADD("http://");
  2546.                                       if (!opt->passprivacy) {
  2547.                                         HT_ADD_HTMLESCAPED(adr);     // Password
  2548.                                       } else {
  2549.                                         HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2550.                                       }
  2551.                                       if (*fil!='/')
  2552.                                         HT_ADD("/");
  2553.                                       HT_ADD_HTMLESCAPED(fil);
  2554.                                     } else {
  2555.                                       char* aut = strstr(adr, "//");
  2556.                                       if (aut) {
  2557.                                         char tmp[256];
  2558.                                         tmp[0]='\0';
  2559.                                         strncatbuff(tmp, adr, (int) (aut - adr) + 2);   // scheme
  2560.                                         HT_ADD(tmp);
  2561.                                         if (!opt->passprivacy) {
  2562.                                           HT_ADD_HTMLESCAPED(jump_protocol(adr));          // Password
  2563.                                         } else {
  2564.                                           HT_ADD_HTMLESCAPED(jump_identification(adr));     // No Password
  2565.                                         }
  2566.                                         if (*fil!='/')
  2567.                                           HT_ADD("/");
  2568.                                         HT_ADD_HTMLESCAPED(fil);
  2569.                                       }
  2570.                                     }
  2571.                                     //
  2572.  
  2573.                                   }
  2574.                                 }
  2575.  
  2576.                                 // Θcrire fichier?
  2577.                                 if (verif_external(cat_nb,1)) {
  2578.                                   //if (!fexist(fconcat(opt->path_html,cat_name))) {
  2579.                                   FILE* fp = filecreate(fconcat(opt->path_html,cat_name));
  2580.                                   if (fp) {
  2581.                                     if (cat_data_len==0) {   // texte
  2582.                                       verif_backblue(opt,opt->path_html);
  2583.                                       fprintf(fp,"%s%s","<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"LF,cat_data);
  2584.                                     } else {                    // data
  2585.                                       fwrite(cat_data,cat_data_len,1,fp);
  2586.                                     }
  2587.                                     fclose(fp);
  2588.                                     usercommand(opt,0,NULL,fconcat(opt->path_html,cat_name),"","");
  2589.                                   }
  2590.                                 }
  2591.                               }  else {    // Θcrire normalement le nom de fichier
  2592.                                 HT_ADD("http://");
  2593.                                 if (!opt->passprivacy) {
  2594.                                   HT_ADD_HTMLESCAPED(adr);       // Password
  2595.                                 } else {
  2596.                                   HT_ADD_HTMLESCAPED(jump_identification(adr));       // No Password
  2597.                                 }
  2598.                                 if (*fil!='/')
  2599.                                   HT_ADD("/");
  2600.                                 HT_ADD_HTMLESCAPED(fil);
  2601.                               }// patcher?
  2602.                             }  // external
  2603.                           } else {  // que le nom de fichier (classe java)
  2604.                             // en gros recopie de plus bas: copier codebase et base
  2605.                             if (p_flush) {
  2606.                               char BIGSTK tempo[HTS_URLMAXSIZE*2];    // <-- ajoutΘ
  2607.                               char BIGSTK tempo_pat[HTS_URLMAXSIZE*2];
  2608.  
  2609.                               // Calculer chemin
  2610.                               tempo_pat[0]='\0';
  2611.                               strcpybuff(tempo,fil);  // <-- ajoutΘ
  2612.                               {
  2613.                                 char* a=strrchr(tempo,'/');
  2614.  
  2615.                                 // Example: we converted code="x.y.z.foo.class" into "x/y/z/foo.class"
  2616.                                 // we have to do the contrary now
  2617.                                 if (add_class_dots_to_patch>0) {
  2618.                                   while( (add_class_dots_to_patch>0) && (a) ) {
  2619.                                     *a='.';     // convert "false" java / into .
  2620.                                     add_class_dots_to_patch--;
  2621.                                     a=strrchr(tempo,'/');
  2622.                                   }
  2623.                                   // if add_class_dots_to_patch, this is because there is a problem!!
  2624.                                   if (add_class_dots_to_patch) {
  2625.                                     if (opt->errlog) {
  2626.                                       fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Error: can not rewind java path %s, check html code"LF,tempo);
  2627.                                       test_flush;
  2628.                                     }
  2629.                                   }
  2630.                                 }
  2631.  
  2632.                                 // Cut path/filename
  2633.                                 if (a) {
  2634.                                   char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  2635.                                   strcpybuff(tempo2,a+1);         // FICHIER
  2636.                                   strncatbuff(tempo_pat,tempo,(int) (a - tempo)+1);  // chemin
  2637.                                   strcpybuff(tempo,tempo2);                     // fichier
  2638.                                 }
  2639.                               }
  2640.  
  2641.                               // Θrire codebase="chemin"
  2642.                               if ((opt->getmode & 1) && (ptr>0)) {
  2643.                                 char BIGSTK tempo4[HTS_URLMAXSIZE*2];
  2644.                                 tempo4[0]='\0';
  2645.  
  2646.                                 if (strnotempty(tempo_pat)) {
  2647.                                   HT_ADD("codebase=\"http://");
  2648.                                   if (!opt->passprivacy) {
  2649.                                     HT_ADD_HTMLESCAPED(adr);  // Password
  2650.                                   } else {
  2651.                                     HT_ADD_HTMLESCAPED(jump_identification(adr));  // No Password
  2652.                                   }
  2653.                                   if (*tempo_pat!='/') HT_ADD("/");
  2654.                                   HT_ADD(tempo_pat);
  2655.                                   HT_ADD("\" ");
  2656.                                 }
  2657.  
  2658.                                 strncatbuff(tempo4,lastsaved,(int) (p_flush - lastsaved));
  2659.                                 HT_ADD(tempo4);    // refresh code="
  2660.                                 HT_ADD(tempo);
  2661.                               }
  2662.                             }
  2663.                           }
  2664.                         }
  2665.                         lastsaved=eadr-1;
  2666.                       }
  2667.                       /*
  2668.                       else if (opt->urlmode==1) {    // ABSOLU, c'est le cas le moins courant
  2669.                       //  NE FONCTIONNE PAS!!  (et est inutile)
  2670.                       if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2671.                       // Θcrire le lien modifiΘ, absolu
  2672.                       HT_ADD("file:");
  2673.                       if (*save=='/')
  2674.                       HT_ADD(save+1)
  2675.                       else
  2676.                       HT_ADD(save)
  2677.                       }
  2678.                       lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2679.                       }
  2680.                       */
  2681.                       else if (opt->mimehtml) {
  2682.                         char BIGSTK buff[HTS_URLMAXSIZE*3];
  2683.                         HT_ADD("cid:");
  2684.                         strcpybuff(buff, adr);
  2685.                         strcatbuff(buff, fil);
  2686.                         escape_in_url(buff);
  2687.                         { char* a = buff; while((a = strchr(a, '%'))) { *a = 'X'; a++; } }
  2688.                         HT_ADD_HTMLESCAPED(buff);
  2689.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2690.                       }
  2691.                       else if (opt->urlmode==3) {    // URI absolue /
  2692.                         if ((opt->getmode & 1) && (ptr>0)) {    // ecrire les html
  2693.                           HT_ADD_HTMLESCAPED(fil);
  2694.                         }
  2695.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2696.                       }
  2697.                       else if (opt->urlmode==2) {  // RELATIF
  2698.                         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2699.                         tempo[0]='\0';
  2700.                         // calculer le lien relatif
  2701.  
  2702.                         if (lienrelatif(tempo,save,relativesavename)==0) {
  2703.                           if (!in_media) {    // In media (such as real audio): don't patch
  2704.                                                         /* Never escape high-chars (we don't know the encoding!!) */
  2705.                                                         escape_uri_utf(tempo);
  2706.                             //if (!no_esc_utf)
  2707.                             //  escape_uri(tempo);     // escape with %xx
  2708.                             //else {
  2709.                             //  /* No escaping at all - remaining upper chars will be escaped below */
  2710.                             //  /* FIXME - Should be done in all local cases */
  2711.                             //  //x_escape_html(tempo);
  2712.                             //  //escape_uri_utf(tempo);     // FIXME - escape with %xx
  2713.                             //  //escape_uri(tempo);     // escape with %xx
  2714.                             //}
  2715.                           }
  2716.                           if ((opt->debug>1) && (opt->log!=NULL)) {
  2717.                             fspc(opt->log,"debug"); fprintf(opt->log,"relative link at %s build with %s and %s: %s"LF,adr,save,relativesavename,tempo);
  2718.                             test_flush;
  2719.                           }
  2720.  
  2721.                           // lien applet (code) - il faut placer un codebase avant
  2722.                           if (p_type==-1) {  // que le nom de fichier
  2723.  
  2724.                             if (p_flush) {
  2725.                               char BIGSTK tempo_pat[HTS_URLMAXSIZE*2];
  2726.                               tempo_pat[0]='\0';
  2727.                               {
  2728.                                 char* a=strrchr(tempo,'/');
  2729.  
  2730.                                 // Example: we converted code="x.y.z.foo.class" into "x/y/z/foo.class"
  2731.                                 // we have to do the contrary now
  2732.                                 if (add_class_dots_to_patch>0) {
  2733.                                   while( (add_class_dots_to_patch>0) && (a) ) {
  2734.                                     *a='.';     // convert "false" java / into .
  2735.                                     add_class_dots_to_patch--;
  2736.                                     a=strrchr(tempo,'/');
  2737.                                   }
  2738.                                   // if add_class_dots_to_patch, this is because there is a problem!!
  2739.                                   if (add_class_dots_to_patch) {
  2740.                                     if (opt->errlog) {
  2741.                                       fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Error: can not rewind java path %s, check html code"LF,tempo);
  2742.                                       test_flush;
  2743.                                     }
  2744.                                   }
  2745.                                 }
  2746.  
  2747.                                 if (a) {
  2748.                                   char BIGSTK tempo2[HTS_URLMAXSIZE*2];
  2749.                                   strcpybuff(tempo2,a+1);
  2750.                                   strncatbuff(tempo_pat,tempo,(int) (a - tempo)+1);  // chemin
  2751.                                   strcpybuff(tempo,tempo2);                     // fichier
  2752.                                 }
  2753.                               }
  2754.  
  2755.                               // Θrire codebase="chemin"
  2756.                               if ((opt->getmode & 1) && (ptr>0)) {
  2757.                                 char BIGSTK tempo4[HTS_URLMAXSIZE*2];
  2758.                                 tempo4[0]='\0';
  2759.  
  2760.                                 if (strnotempty(tempo_pat)) {
  2761.                                   HT_ADD("codebase=\"");
  2762.                                   HT_ADD_HTMLESCAPED(tempo_pat);
  2763.                                   HT_ADD("\" ");
  2764.                                 }
  2765.  
  2766.                                 strncatbuff(tempo4,lastsaved,(int) (p_flush - lastsaved));
  2767.                                 HT_ADD(tempo4);    // refresh code="
  2768.                               }
  2769.                             }
  2770.                             //lastsaved=adr;    // dernier Θcrit+1
  2771.                           }                              
  2772.  
  2773.                           if ((opt->getmode & 1) && (ptr>0)) {
  2774.                             // Θcrire le lien modifiΘ, relatif
  2775.                             // Note: escape all chars, even >127 (no UTF)
  2776.                             HT_ADD_HTMLESCAPED_FULL(tempo);
  2777.  
  2778.                             // Add query-string, for informational purpose only
  2779.                             // Useless, because all parameters-pages are saved into different targets
  2780.                             if (opt->includequery) {
  2781.                               char* a=strchr(lien,'?');
  2782.                               if (a) {
  2783.                                 HT_ADD_HTMLESCAPED(a);
  2784.                               }
  2785.                             }
  2786.                           }
  2787.                           lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  2788.                         } else {
  2789.                           if (opt->errlog) {
  2790.                             fprintf(opt->errlog,"Error building relative link %s and %s"LF,save,relativesavename);
  2791.                             test_flush;
  2792.                           }
  2793.                         }
  2794.                       }  // sinon le lien sera Θcrit normalement
  2795.  
  2796.  
  2797. #if 0
  2798.                       if (fexist(save)) {    // le fichier existe..
  2799.                         adr[0]='\0';
  2800.                         //if ((opt->debug>0) && (opt->log!=NULL)) {
  2801.                         if (opt->errlog) {
  2802.                           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Link has already been written on disk, cancelled: %s"LF,save);
  2803.                           test_flush;
  2804.                         }
  2805.                       }
  2806. #endif                            
  2807.  
  2808.                       /* Security check */
  2809.                       if (strlen(save) >= HTS_URLMAXSIZE) {
  2810.                         adr[0]='\0';
  2811.                         if (opt->errlog) {
  2812.                           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Link is too long: %s"LF,save);
  2813.                           test_flush;
  2814.                         }
  2815.                       }
  2816.  
  2817.                       if ((adr[0]!='\0') && (p_type!=2) && (p_type!=-2) && (forbidden_url!=1) ) {  // si le fichier n'existe pas, ajouter α la liste                            
  2818.                         // n'y a-t-il pas trop de liens?
  2819.                         if (lien_tot+1 >= lien_max-4) {    // trop de liens!
  2820.                           printf("PANIC! : Too many URLs : >%d [%d]\n",lien_tot,__LINE__);
  2821.                           if (opt->errlog) {
  2822.                             fprintf(opt->errlog,LF"Too many URLs, giving up..(>%d)"LF,lien_max);
  2823.                             fprintf(opt->errlog,"To avoid that: use #L option for more links (example: -#L1000000)"LF);
  2824.                             test_flush;
  2825.                           }
  2826.                           if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2827.                           XH_uninit;   // dΘsallocation mΘmoire & buffers
  2828.                           return -1;
  2829.  
  2830.                         } else {    // noter le lien sur la listes des liens α charger
  2831.                           int pass_fix,dejafait=0;
  2832.  
  2833.                           // Calculer la prioritΘ de ce lien
  2834.                           if ((opt->getmode & 4)==0) {    // traiter html aprΦs
  2835.                             pass_fix=0;
  2836.                           } else {    // vΘrifier que ce n'est pas un !html
  2837.                             if (!ishtml(fil))
  2838.                               pass_fix=1;        // prioritΘ infΘrieure (traiter aprΦs)
  2839.                             else
  2840.                               pass_fix=max(0,numero_passe);    // prioritΘ normale
  2841.                           }
  2842.  
  2843.                           /* If the file seems to be an html file, get depth-1 */
  2844.                           /*
  2845.                           if (strnotempty(save)) {
  2846.                           if (ishtml(save) == 1) {
  2847.                           // descore_prio = 2;
  2848.                           } else {
  2849.                           // descore_prio = 1;
  2850.                           }
  2851.                           }
  2852.                           */
  2853.  
  2854.                           // vΘrifier que le lien n'a pas dΘja ΘtΘ notΘ
  2855.                           // si c'est le cas, alors il faut s'assurer que la prioritΘ associΘe
  2856.                           // au fichier est la plus grande des deux prioritΘs
  2857.                           //
  2858.                           // On part de la fin et on essaye de se presser (Θconomise temps machine)
  2859.                           {
  2860.                             int i=hash_read(hash,save,"",0,opt->urlhack);      // lecture type 0 (sav)
  2861.                             if (i>=0) {
  2862.                               if ((opt->debug>1) && (opt->log!=NULL)) {
  2863.                                 if (
  2864.                                   strcmp(adr, liens[i]->adr) != 0 
  2865.                                   || strcmp(fil, liens[i]->fil) != 0
  2866.                                   ) {
  2867.                                     fspc(opt->log,"debug"); fprintf(opt->log,"merging similar links %s%s and %s%s"LF,adr,fil,liens[i]->adr,liens[i]->fil);
  2868.                                     test_flush;
  2869.                                   }
  2870.                               }
  2871.                               liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth - 1);
  2872.                               dejafait=1;
  2873.                             }
  2874.                           }
  2875.  
  2876.                           // le lien n'a jamais ΘtΘ crΘΘ.
  2877.                           // cette fois ci, on le crΘe!
  2878.                           if (!dejafait) {                                
  2879.                             //
  2880.                             // >>>> CREER LE LIEN <<<<
  2881.                             //
  2882.                             // enregistrer lien α charger
  2883.                             //liens[lien_tot]->adr[0]=liens[lien_tot]->fil[0]=liens[lien_tot]->sav[0]='\0';
  2884.                             // mΩme adresse: l'objet pΦre est l'objet pΦre de l'actuel
  2885.  
  2886.                                                         // DEBUT ROBOTS.TXT AJOUT
  2887.                                                         if (!just_test_it) {
  2888.                                                             if ((!strfield(adr,"ftp://"))         // non ftp
  2889.                                                                 && (!strfield(adr,"file://")) 
  2890. #if HTS_USEMMS
  2891.                                                                 && (!strfield(adr,"mms://")) 
  2892. #endif
  2893.                                                                 ) 
  2894.                                                             {    // non file
  2895.                                                                 if (opt->robots) {    // rΘcupΘrer robots
  2896.                                                                     if (ishtml(fil)!=0) {                       // pas la peine pour des fichiers isolΘs
  2897.                                                                         if (checkrobots(_ROBOTS,adr,"") != -1) {    // robots.txt ?
  2898.                                                                             checkrobots_set(_ROBOTS ,adr,"");          // ajouter entrΘe vide
  2899.                                                                             if (checkrobots(_ROBOTS,adr,"") == -1) {    // robots.txt ?
  2900.                                                                                 // enregistrer robots.txt (MACRO)
  2901.                                                                                 liens_record(adr,"/robots.txt","","","");
  2902.                                                                                 if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  2903.                                                                                     printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  2904.                                                                                     if (opt->errlog) { 
  2905.                                                                                         fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  2906.                                                                                         test_flush;
  2907.                                                                                     }
  2908.                                                                                     if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2909.                                                                                     XH_uninit;    // dΘsallocation mΘmoire & buffers
  2910.                                                                                     return -1;
  2911.                                                                                 }  
  2912.                                                                                 liens[lien_tot]->testmode=0;          // pas mode test
  2913.                                                                                 liens[lien_tot]->link_import=0;       // pas mode import     
  2914.                                                                                 liens[lien_tot]->premier=lien_tot;
  2915.                                                                                 liens[lien_tot]->precedent=ptr;
  2916.                                                                                 liens[lien_tot]->depth=0;
  2917.                                                                                 liens[lien_tot]->pass2=max(0,numero_passe);
  2918.                                                                                 liens[lien_tot]->retry=0;
  2919.                                                                                 lien_tot++;  // UN LIEN DE PLUS
  2920. #if DEBUG_ROBOTS
  2921.                                                                                 printf("robots.txt: added file robots.txt for %s\n",adr);
  2922. #endif
  2923.                                                                                 if ((opt->debug>1) && (opt->log!=NULL)) {
  2924.                                                                                     fspc(opt->log,"debug"); fprintf(opt->log,"robots.txt added at %s"LF,adr);
  2925.                                                                                     test_flush;
  2926.                                                                                 }
  2927.                                                                             } else {
  2928.                                                                                 if (opt->errlog) {   
  2929.                                                                                     fprintf(opt->errlog,"Unexpected robots.txt error at %d"LF,__LINE__);
  2930.                                                                                     test_flush;
  2931.                                                                                 }
  2932.                                                                             }
  2933.                                                                         }
  2934.                                                                     }
  2935.                                                                 }
  2936.                                                             }
  2937.                                                         }
  2938.                                                         // FIN ROBOTS.TXT AJOUT
  2939.  
  2940.                                                         // enregistrer (MACRO)
  2941.                                                         liens_record(adr,fil,save,former_adr,former_fil);
  2942.                                                         if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  2943.                                                             printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  2944.                                                             if (opt->errlog) { 
  2945.                                                                 fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  2946.                                                                 test_flush;
  2947.                                                             }
  2948.                               if ((opt->getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  2949.                               XH_uninit;    // dΘsallocation mΘmoire & buffers
  2950.                               return -1;
  2951.                             }  
  2952.  
  2953.                             // mode test?
  2954.                             if (!just_test_it)
  2955.                               liens[lien_tot]->testmode=0;          // pas mode test
  2956.                             else
  2957.                               liens[lien_tot]->testmode=1;          // mode test
  2958.                             if (!import_done)
  2959.                               liens[lien_tot]->link_import=0;       // pas mode import
  2960.                             else
  2961.                               liens[lien_tot]->link_import=1;       // mode import
  2962.                             // Θcrire autres paramΦtres de la structure-lien
  2963.                             if ((meme_adresse) && (!import_done) && (liens[ptr]->premier != 0))
  2964.                               liens[lien_tot]->premier=liens[ptr]->premier;
  2965.                             else    // sinon l'objet pΦre est le prΘcΘdent lui mΩme
  2966.                               liens[lien_tot]->premier=lien_tot;
  2967.                             // liens[lien_tot]->premier=ptr;
  2968.  
  2969.                             liens[lien_tot]->precedent=ptr;
  2970.                             // noter la prioritΘ
  2971.                             if (!set_prio_to)
  2972.                               liens[lien_tot]->depth=liens[ptr]->depth - 1;
  2973.                             else
  2974.                               liens[lien_tot]->depth=max(0,min(liens[ptr]->depth-1,set_prio_to-1));         // PRIORITE NULLE (catch page)
  2975.                             // noter pass
  2976.                             liens[lien_tot]->pass2=pass_fix;
  2977.                             liens[lien_tot]->retry=opt->retry;
  2978.  
  2979.                             //strcpybuff(liens[lien_tot]->adr,adr);
  2980.                             //strcpybuff(liens[lien_tot]->fil,fil);
  2981.                             //strcpybuff(liens[lien_tot]->sav,save); 
  2982.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  2983.                               if (!just_test_it) {
  2984.                                 fspc(opt->log,"debug"); fprintf(opt->log,"OK, NOTE: %s%s -> %s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav);
  2985.                               } else {
  2986.                                 fspc(opt->log,"debug"); fprintf(opt->log,"OK, TEST: %s%s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil);
  2987.                               }
  2988.                               test_flush;
  2989.                             }
  2990.  
  2991.                             lien_tot++;  // UN LIEN DE PLUS
  2992.                           } else { // if !dejafait
  2993.                             if ((opt->debug>1) && (opt->log!=NULL)) {
  2994.                               fspc(opt->log,"debug"); fprintf(opt->log,"link has already been recorded, cancelled: %s"LF,save);
  2995.                               test_flush;
  2996.                             }
  2997.  
  2998.                           }
  2999.  
  3000.  
  3001.                         }   // si pas trop de liens
  3002.                       }   // si adr[0]!='\0'
  3003.  
  3004.  
  3005.                     }  // if adr[0]!='\0' 
  3006.  
  3007.                   }  // if adr[0]!='\0'
  3008.  
  3009.                 }    // if strlen(lien)>0
  3010.  
  3011.               }   // if ok==0      
  3012.  
  3013.               assertf(eadr - adr >= 0);       // Should not go back
  3014.               if (eadr > adr) {
  3015.                 INCREMENT_CURRENT_ADR(eadr - 1 - adr);
  3016.               }
  3017.               // adr=eadr-1;  // ** sauter
  3018.  
  3019.               /* We skipped bytes and skip the " : reset state */
  3020.               /*if (inscript) {
  3021.               inscript_state_pos = INSCRIPT_START;
  3022.               }*/
  3023.  
  3024.           }  // if (p) 
  3025.  
  3026.         }  // si '<' ou '>'
  3027.  
  3028.         // plus loin
  3029.         adr++;      // automate will be checked next loop
  3030.  
  3031.  
  3032.         /* Otimization: if we are scanning in HTML data (not in tag or script), 
  3033.         then jump to the next starting tag */
  3034.         if (ptr>0) {
  3035.           if ( (!intag)         /* Not in tag */
  3036.             && (!inscript)      /* Not in (java)script */
  3037.             && (!in_media)      /* Not in media */
  3038.             && (!incomment)     /* Not in comment (<!--) */
  3039.             && (!inscript_tag)  /* Not in tag with script inside */
  3040.             ) 
  3041.           {
  3042.             /* Not at the end */
  3043.             if (( ((int) (adr - r->adr)) ) < r->size) {
  3044.               /* Not on a starting tag yet */
  3045.               if (*adr != '<') {
  3046.                 /* strchr does not well behave with null chrs.. */
  3047.                 /* char* adr_next = strchr(adr,'<'); */
  3048.                 char* adr_next = adr;
  3049.                 while(*adr_next != '<' && (adr_next - r->adr) < r->size ) {
  3050.                   adr_next++;
  3051.                 }
  3052.                 /* Jump to near end (index hack) */
  3053.                 if (!adr_next || *adr_next != '<') {
  3054.                   if (
  3055.                     ( (int)(adr - r->adr) < (r->size - 4)) 
  3056.                     &&
  3057.                     (r->size > 4)
  3058.                     ) {
  3059.                       adr = r->adr + r->size - 2;
  3060.                     }
  3061.                 } else {
  3062.                   adr = adr_next;
  3063.                 }
  3064.               }
  3065.             }
  3066.           }
  3067.         }
  3068.  
  3069.         // ----------
  3070.         // Θcrire peu α peu
  3071.         if ((opt->getmode & 1) && (ptr>0)) HT_ADD_ADR;
  3072.         lastsaved=adr;    // dernier Θcrit+1
  3073.         // ----------
  3074.  
  3075.         // Checks
  3076.         if (back_add_stats != opt->state.back_add_stats) {
  3077.           back_add_stats = opt->state.back_add_stats;
  3078.  
  3079.           // Check max time
  3080.           if (!back_checkmirror(opt)) {
  3081.             adr = r->adr + r->size;
  3082.           }
  3083.         }
  3084.  
  3085.         // pour les stats du shell si parsing trop long
  3086. #if HTS_ANALYSTE
  3087.         if (r->size)
  3088.           _hts_in_html_done=(100 * ((int) (adr - r->adr)) ) / (int)(r->size);
  3089.         if (_hts_in_html_poll) {
  3090.           _hts_in_html_poll=0;
  3091.           // temps α attendre, et remplir autant que l'on peut le cache (backing)
  3092.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);        
  3093.           back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3094.  
  3095.           // Transfer rate
  3096.           engine_stats();
  3097.  
  3098.           // Refresh various stats
  3099.           HTS_STAT.stat_nsocket=back_nsoc(sback);
  3100.           HTS_STAT.stat_errors=fspc(NULL,"error");
  3101.           HTS_STAT.stat_warnings=fspc(NULL,"warning");
  3102.           HTS_STAT.stat_infos=fspc(NULL,"info");
  3103.           HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3104.           HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3105.  
  3106.           if (!hts_htmlcheck_loop(sback->lnk, sback->count, 0,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3107.             if (opt->errlog) {
  3108.               fspc(opt->errlog,"info"); fprintf(opt->errlog,"Exit requested by shell or user"LF);
  3109.               test_flush;
  3110.             } 
  3111.             *stre->exit_xh_=1;  // exit requested
  3112.             XH_uninit;
  3113.             return -1;
  3114.             //adr = r->adr + r->size;  // exit
  3115.           } else if (_hts_cancel==1) {
  3116.             // adr = r->adr + r->size;  // exit
  3117.             nofollow=1;               // moins violent
  3118.             _hts_cancel=0;
  3119.           }
  3120.         }
  3121.  
  3122.         // refresh the backing system each 2 seconds
  3123.         if (engine_stats()) {
  3124.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);        
  3125.           back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3126.         }
  3127. #endif
  3128.       } while(( ((int) (adr - r->adr)) ) < r->size);
  3129. #if HTS_ANALYSTE
  3130.       _hts_in_html_parsing=0;  // flag
  3131.       _hts_cancel=0;           // pas de cancel
  3132. #endif
  3133.       if ((opt->getmode & 1) && (ptr>0)) {
  3134.         {
  3135.           char* cAddr = ht_buff;
  3136.           int cSize = ht_len;
  3137.           if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3138.             fspc(opt->log,"info"); fprintf(opt->log,"engine: postprocess-html: %s%s"LF, urladr, urlfil);
  3139.           }
  3140.           if (hts_htmlcheck_postprocess(&cAddr, &cSize, urladr, urlfil) == 1) {
  3141.             ht_buff = cAddr;
  3142.             ht_len = cSize;
  3143.           }
  3144.         }
  3145.  
  3146.         /* Flush and save to disk */
  3147.         HT_ADD_END;    // achever
  3148.       }
  3149.       //
  3150.       //
  3151.       //
  3152.     }  // if !error
  3153.  
  3154.  
  3155.     if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3156.     // sauver fichier
  3157.     //structcheck(savename);
  3158.     //filesave(opt,r->adr,r->size,savename);
  3159.  
  3160. #if HTS_ANALYSTE
  3161.   }  // analyse OK
  3162. #endif
  3163.  
  3164.   /* Apply changes */
  3165.   ENGINE_SAVE_CONTEXT();
  3166.  
  3167.   return 0;
  3168. }
  3169.  
  3170.  
  3171.  
  3172.  
  3173. /*
  3174. Check 301, 302, .. statuscodes (moved)
  3175. */
  3176. int hts_mirror_check_moved(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  3177.   /* Load engine variables */
  3178.   ENGINE_LOAD_CONTEXT();  
  3179.  
  3180.   // DEBUT rattrapage des 301,302,307..
  3181.   // ------------------------------------------------------------
  3182.   if (!error) {
  3183.     ////////{
  3184.     // on a chargΘ un fichier en plus
  3185.     // if (!error) stat_loaded+=r.size;
  3186.  
  3187.     // ------------------------------------------------------------
  3188.     // Rattrapage des 301,302,307 (moved) et 412,416 - les 304 le sont dans le backing 
  3189.     // ------------------------------------------------------------
  3190.     if (HTTP_IS_REDIRECT(r->statuscode)) {          
  3191.         //if (r->adr!=NULL) {   // adr==null si fichier direct. [catch: davename normalement si cgi]
  3192.         //int i=0;
  3193.         char *rn=NULL;
  3194.         // char* p;
  3195.  
  3196.         if ( (opt->debug>0) && (opt->errlog!=NULL) ) {
  3197.           //if (opt->errlog) {
  3198.           fspc(opt->errlog,"warning"); fprintf(opt->errlog,"%s for %s%s"LF,r->msg,urladr,urlfil);
  3199.           test_flush;
  3200.         }
  3201.  
  3202.  
  3203.         {
  3204.           char BIGSTK mov_url[HTS_URLMAXSIZE*2],mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  3205.           int get_it=0;         // ne pas prendre le fichier α la mΩme adresse par dΘfaut
  3206.           int reponse=0;
  3207.           mov_url[0]='\0'; mov_adr[0]='\0'; mov_fil[0]='\0';
  3208.           //
  3209.  
  3210.           strcpybuff(mov_url,r->location);
  3211.  
  3212.           // url qque -> adresse+fichier
  3213.           if ((reponse=ident_url_relatif(mov_url,urladr,urlfil,mov_adr,mov_fil))>=0) {                        
  3214.             int set_prio_to=0;    // pas de priotitΘ fixΘd par wizard
  3215.  
  3216.             // check whether URLHack is harmless or not
  3217.             if (opt->urlhack) {
  3218.               char BIGSTK n_adr[HTS_URLMAXSIZE*2], n_fil[HTS_URLMAXSIZE*2];
  3219.               char BIGSTK pn_adr[HTS_URLMAXSIZE*2], pn_fil[HTS_URLMAXSIZE*2];
  3220.               n_adr[0] = n_fil[0] = '\0';
  3221.               (void) adr_normalized(mov_adr, n_adr);
  3222.               (void) fil_normalized(mov_fil, n_fil);
  3223.               (void) adr_normalized(urladr, pn_adr);
  3224.               (void) fil_normalized(urlfil, pn_fil);
  3225.               if (strcasecmp(n_adr, pn_adr) == 0 && strcasecmp(n_fil, pn_fil) == 0) {
  3226.                 if (opt->errlog) {
  3227.                   fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Redirected link is identical because of 'URL Hack' option: %s%s and %s%s"LF, urladr, urlfil, mov_adr, mov_fil);
  3228.                   test_flush;
  3229.                 }
  3230.               }
  3231.             }
  3232.  
  3233.             //if (ident_url_absolute(mov_url,mov_adr,mov_fil)!=-1) {    // ok URL reconnue
  3234.             // c'est (en gros) la mΩme URL..
  3235.             // si c'est un problΦme de casse dans le host c'est que le serveur est buggΘ
  3236.             // ("RFC says.." : host name IS case insensitive)
  3237.             if ((strfield2(mov_adr,urladr)!=0) && (strfield2(mov_fil,urlfil)!=0)) {  // identique α casse prΦs
  3238.               // on tourne en rond
  3239.               if (strcmp(mov_fil,urlfil)==0) {
  3240.                 error=1;
  3241.                 get_it=-1;        // ne rien faire
  3242.                 if (opt->errlog) {
  3243.                   fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Can not bear crazy server (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3244.                   test_flush;
  3245.                 }
  3246.               } else {    // mauvaise casse, effacer entrΘe dans la pile et rejouer une fois
  3247.                 get_it=1;
  3248.               }
  3249.             } else {        // adresse diffΘrente
  3250.               if (ishtml(mov_url)==0) {   // pas mΩme adresse MAIS c'est un fichier non html (pas de page moved possible)
  3251.                 // -> on prend α cette adresse, le lien sera enregistrΘ avec lien_record() (hash)
  3252.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3253.                   fspc(opt->log,"debug"); fprintf(opt->log,"wizard link test for moved file at %s%s.."LF,mov_adr,mov_fil);
  3254.                   test_flush;
  3255.                 }
  3256.                 // acceptΘ?
  3257.                 if (hts_acceptlink(opt,ptr,lien_tot,liens,
  3258.                   mov_adr,mov_fil,
  3259.                   NULL, NULL,
  3260.                   &set_prio_to,
  3261.                   NULL) != 1) {                /* nouvelle adresse non refusΘe ? */
  3262.                     get_it=1;
  3263.                     if ((opt->debug>1) && (opt->log!=NULL)) {
  3264.                       fspc(opt->log,"debug"); fprintf(opt->log,"moved link accepted: %s%s"LF,mov_adr,mov_fil);
  3265.                       test_flush;
  3266.                     }
  3267.                   }
  3268.               } /* sinon traitΘ normalement */
  3269.             }
  3270.  
  3271.             //if ((strfield2(mov_adr,urladr)!=0) && (strfield2(mov_fil,urlfil)!=0)) {  // identique α casse prΦs
  3272.             if (get_it==1) {
  3273.               // court-circuiter le reste du traitement
  3274.               // et reculer pour mieux sauter
  3275.               if (opt->errlog) {
  3276.                 fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Warning moved treated for %s%s (real one is %s%s)"LF,urladr,urlfil,mov_adr,mov_fil);
  3277.                 test_flush;
  3278.               }          
  3279.               // canceller lien actuel
  3280.               error=1;
  3281.               strcpybuff(liens[ptr]->adr,"!");  // caractΦre bidon (invalide hash)
  3282.               // noter NOUVEAU lien
  3283.               //xxc xxc
  3284.               //  set_prio_to=0+1;  // protection if the moved URL is an html page!!
  3285.               //xxc xxc
  3286.               {
  3287.                 char BIGSTK mov_sav[HTS_URLMAXSIZE*2];
  3288.                 // calculer lien et Θventuellement modifier addresse/fichier
  3289.                 if (url_savename(mov_adr,mov_fil,mov_sav,NULL,NULL,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL)!=-1) { 
  3290.                   if (hash_read(hash,mov_sav,"",0,0)<0) {      // n'existe pas dΘja
  3291.                     // enregistrer lien (MACRO) avec SAV IDENTIQUE
  3292.                     liens_record(mov_adr,mov_fil,liens[ptr]->sav,"","");
  3293.                     //liens_record(mov_adr,mov_fil,mov_sav,"","");
  3294.                     if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3295.                       // mode test?
  3296.                       liens[lien_tot]->testmode=liens[ptr]->testmode;
  3297.                       liens[lien_tot]->link_import=0;       // mode normal
  3298.                       if (!set_prio_to)
  3299.                         liens[lien_tot]->depth=liens[ptr]->depth;
  3300.                       else
  3301.                         liens[lien_tot]->depth=max(0,min(set_prio_to-1,liens[ptr]->depth));       // PRIORITE NULLE (catch page)
  3302.                       liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3303.                       liens[lien_tot]->retry=liens[ptr]->retry;
  3304.                       liens[lien_tot]->premier=liens[ptr]->premier;
  3305.                       liens[lien_tot]->precedent=liens[ptr]->precedent;
  3306.                       lien_tot++;
  3307.                     } else {  // oups erreur, plus de mΘmoire!!
  3308.                       printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3309.                       if (opt->errlog) {
  3310.                         fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3311.                         test_flush;
  3312.                       }
  3313.                       //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3314.                       XH_uninit;    // dΘsallocation mΘmoire & buffers
  3315.                       return 0;
  3316.                     }
  3317.                   } else {
  3318.                     if ( (opt->debug>0) && (opt->errlog!=NULL) ) {
  3319.                       fspc(opt->errlog,"warning"); fprintf(opt->errlog,"moving %s to an existing file %s"LF,liens[ptr]->fil,urlfil);
  3320.                       test_flush;
  3321.                     }
  3322.                   }
  3323.  
  3324.                 }
  3325.               }
  3326.  
  3327.               //printf("-> %s %s %s\n",liens[lien_tot-1]->adr,liens[lien_tot-1]->fil,liens[lien_tot-1]->sav);
  3328.  
  3329.               // note mΘtaphysique: il se peut qu'il y ait un index.html et un INDEX.HTML
  3330.               // sous DOS ca marche pas trΦs bien... mais comme je suis gΘnial url_savename()
  3331.               // est α mΩme de rΘgler ce problΦme
  3332.             }
  3333.           } // ident_url_xx
  3334.  
  3335.           if (get_it==0) {    // adresse vraiment diffΘrente et potentiellement en html (pas de possibilitΘ de bouger la page tel quel α cause des <img src..> et cie)
  3336.             rn=(char*) calloct(8192,1);
  3337.             if (rn!=NULL) {
  3338.               if (opt->errlog) {
  3339.                 fspc(opt->errlog,"warning"); fprintf(opt->errlog,"File has moved from %s%s to %s"LF,urladr,urlfil,mov_url);
  3340.                 test_flush;
  3341.               }
  3342.               if (!opt->mimehtml) {
  3343.                 escape_uri(mov_url);
  3344.               } else {
  3345.                 char BIGSTK buff[HTS_URLMAXSIZE*3];
  3346.                 strcpybuff(buff, mov_adr);
  3347.                 strcatbuff(buff, mov_fil);
  3348.                 escape_in_url(buff);
  3349.                 { char* a = buff; while((a = strchr(a, '%'))) { *a = 'X'; a++; } }
  3350.                 strcpybuff(mov_url, "cid:");
  3351.                 strcatbuff(mov_url, buff);
  3352.               }
  3353.               // On prΘpare une page qui sautera immΘdiatement sur la bonne URL
  3354.               // Le scanner re-changera, ensuite, cette URL, pour la mirrorer!
  3355.               strcpybuff(rn,"<HTML>"CRLF);
  3356.               strcatbuff(rn,"<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  3357.               strcatbuff(rn,"<HEAD>"CRLF"<TITLE>Page has moved</TITLE>"CRLF"</HEAD>"CRLF"<BODY>"CRLF);
  3358.               strcatbuff(rn,"<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=");
  3359.               strcatbuff(rn,mov_url);    // URL
  3360.               strcatbuff(rn,"\">"CRLF);
  3361.               strcatbuff(rn,"<A HREF=\"");
  3362.               strcatbuff(rn,mov_url);
  3363.               strcatbuff(rn,"\">");
  3364.               strcatbuff(rn,"<B>Click here...</B></A>"CRLF);
  3365.               strcatbuff(rn,"</BODY>"CRLF);
  3366.               strcatbuff(rn,"<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  3367.               strcatbuff(rn,"</HTML>"CRLF);
  3368.  
  3369.               // changer la page
  3370.               if (r->adr) { 
  3371.                 freet(r->adr); 
  3372.                 r->adr=NULL; 
  3373.               }
  3374.               r->adr=rn;
  3375.               r->size=strlen(r->adr);
  3376.               strcpybuff(r->contenttype, "text/html");
  3377.             }
  3378.           }  // get_it==0
  3379.  
  3380.         }     // bloc
  3381.         // erreur HTTP (ex: 404, not found)
  3382.       } else if (
  3383.         (r->statuscode==412)
  3384.         || (r->statuscode==416)
  3385.         ) {    // Precondition Failed, c'est α dire pour nous redemander TOUT le fichier
  3386.           if (fexist(liens[ptr]->sav)) {
  3387.             remove(liens[ptr]->sav);    // Eliminer
  3388.             if (!fexist(liens[ptr]->sav)) {  // Bien ΘliminΘ? (sinon on boucle..)
  3389. #if HDEBUG
  3390.               printf("Partial content NOT up-to-date, reget all file for %s\n",liens[ptr]->sav);
  3391. #endif
  3392.               if ( (opt->debug>1) && (opt->errlog!=NULL) ) {
  3393.                 //if (opt->errlog) {
  3394.                 fspc(opt->errlog,"debug"); fprintf(opt->errlog,"Partial file reget (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3395.                 test_flush;
  3396.               }
  3397.               // enregistrer le MEME lien (MACRO)
  3398.               liens_record(liens[ptr]->adr,liens[ptr]->fil,liens[ptr]->sav,"","");
  3399.               if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3400.                 liens[lien_tot]->testmode=liens[ptr]->testmode;          // mode test?
  3401.                 liens[lien_tot]->link_import=0;       // pas mode import
  3402.                 liens[lien_tot]->depth=liens[ptr]->depth;
  3403.                 liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3404.                 liens[lien_tot]->retry=liens[ptr]->retry;
  3405.                 liens[lien_tot]->premier=liens[ptr]->premier;
  3406.                 liens[lien_tot]->precedent=ptr;
  3407.                 lien_tot++;
  3408.                 //
  3409.                 // canceller lien actuel
  3410.                 error=1;
  3411.                 strcpybuff(liens[ptr]->adr,"!");  // caractΦre bidon (invalide hash)
  3412.                 //
  3413.               } else {  // oups erreur, plus de mΘmoire!!
  3414.                 printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3415.                 if (opt->errlog) {
  3416.                   fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3417.                   test_flush;
  3418.                 }
  3419.                 //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3420.                 XH_uninit;    // dΘsallocation mΘmoire & buffers
  3421.                 return 0;
  3422.               } 
  3423.             } else {
  3424.               if (opt->errlog!=NULL) {
  3425.                 fspc(opt->errlog,"error"); fprintf(opt->errlog,"Can not remove old file %s"LF,urlfil);
  3426.                 test_flush;
  3427.               }
  3428.             }
  3429.           } else {
  3430.             if (opt->errlog!=NULL) {
  3431.               fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Unexpected 412/416 error (%s) for %s%s"LF,r->msg,urladr,urlfil);
  3432.               test_flush;
  3433.             }
  3434.           }
  3435.         } else if (r->statuscode!=200) {
  3436.           int can_retry=0;
  3437.  
  3438.           // cas o∙ l'on peut reessayer
  3439.           switch(r->statuscode) {
  3440.             //case -1: can_retry=1; break;
  3441.           case STATUSCODE_TIMEOUT:
  3442.             if (opt->hostcontrol) {    // timeout et retry ΘpuisΘs
  3443.               if ((opt->hostcontrol & 1) && (liens[ptr]->retry<=0)) {
  3444.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3445.                   fspc(opt->log,"debug"); fprintf(opt->log,"Link banned: %s%s"LF,urladr,urlfil); test_flush;
  3446.                 }
  3447.                 host_ban(opt,liens,ptr,lien_tot,sback,jump_identification(urladr));
  3448.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3449.                   fspc(opt->log,"debug"); fprintf(opt->log,"Info: previous log - link banned: %s%s"LF,urladr,urlfil); test_flush;
  3450.                 }
  3451.               } else can_retry=1;
  3452.             } else can_retry=1;
  3453.             break;
  3454.           case STATUSCODE_SLOW:
  3455.             if ((opt->hostcontrol) && (liens[ptr]->retry<=0)) {    // too slow
  3456.               if (opt->hostcontrol & 2) {
  3457.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3458.                   fspc(opt->log,"debug"); fprintf(opt->log,"Link banned: %s%s"LF,urladr,urlfil); test_flush;
  3459.                 }
  3460.                 host_ban(opt,liens,ptr,lien_tot,sback,jump_identification(urladr));
  3461.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  3462.                   fspc(opt->log,"debug"); fprintf(opt->log,"Info: previous log - link banned: %s%s"LF,urladr,urlfil); test_flush;
  3463.                 }
  3464.               } else can_retry=1;
  3465.             } else can_retry=1;
  3466.             break;
  3467.           case STATUSCODE_CONNERROR:            // connect closed
  3468.             can_retry=1;
  3469.             break;
  3470.           case STATUSCODE_NON_FATAL:            // other (non fatal) error
  3471.             can_retry=1;
  3472.             break;
  3473.           case STATUSCODE_SSL_HANDSHAKE:            // bad SSL handskake
  3474.             can_retry=1;
  3475.             break;
  3476.           case 408: case 409: case 500: case 502: case 504: 
  3477.             can_retry=1;
  3478.             break;
  3479.           }
  3480.  
  3481.           if ( strcmp(liens[ptr]->fil,"/primary") != 0 ) {  // no primary (internal page 0)
  3482.             if ((liens[ptr]->retry<=0) || (!can_retry) ) {  // retry ΘpuisΘs (ou retry impossible)
  3483.               if (opt->errlog) {
  3484.                 if ((opt->retry>0) && (can_retry)){
  3485.                   fspc(opt->errlog,"error"); 
  3486.                   fprintf(opt->errlog,"\"%s\" (%d) after %d retries at link %s%s (from %s%s)"LF,r->msg,r->statuscode,opt->retry,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3487.                 } else {
  3488.                   if (r->statuscode==STATUSCODE_TEST_OK) {    // test OK
  3489.                     if ((opt->debug>0) && (opt->errlog!=NULL)) {
  3490.                       fspc(opt->errlog,"info"); 
  3491.                       fprintf(opt->errlog,"Test OK at link %s%s (from %s%s)"LF,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3492.                     }
  3493.                   } else {
  3494.                     if (strcmp(urlfil,"/robots.txt")) {       // ne pas afficher d'infos sur robots.txt par dΘfaut
  3495.                       fspc(opt->errlog,"error"); 
  3496.                       fprintf(opt->errlog,"\"%s\" (%d) at link %s%s (from %s%s)"LF,r->msg,r->statuscode,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3497.                     } else {
  3498.                       if (opt->debug>1) {
  3499.                         fspc(opt->errlog,"info"); fprintf(opt->errlog,"No robots.txt rules at %s"LF,urladr);
  3500.                         test_flush;
  3501.                       }
  3502.                     }
  3503.                   }
  3504.                 }
  3505.                 test_flush;
  3506.               }
  3507.  
  3508.               // NO error in trop level
  3509.               // due to the "no connection -> previous restored" hack
  3510.               // This prevent the engine from wiping all data if the website has been deleted (or moved)
  3511.               // since last time (which is quite annoying)
  3512.               if (liens[ptr]->precedent != 0) {
  3513.                 // ici on teste si on doit enregistrer la page tout de mΩme
  3514.                 if (opt->errpage) {
  3515.                   store_errpage=1;
  3516.                 }
  3517.               } else {
  3518.                 if (strcmp(urlfil,"/robots.txt") != 0) {
  3519.                   /*
  3520.                   This is an error caused by a link entered by the user
  3521.                   That is, link(s) entered by user are invalid (404, 500, connect error, proxy error->.)
  3522.                   If all links entered are invalid, the session failed and we will attempt to restore
  3523.                   the previous one
  3524.                   Example: Try to update a website which has been deleted remotely: this may delete
  3525.                   the website locally, which is really not desired (especially if the website disappeared!)
  3526.                   With this hack, the engine won't wipe local files (how clever)
  3527.                   */
  3528.                   HTS_STAT.stat_errors_front++;
  3529.                 }
  3530.               }
  3531.  
  3532.             } else {    // retry!!
  3533.               if (opt->debug>0 && opt->errlog != NULL) {  // on fera un alert si le retry Θchoue               
  3534.                 fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Retry after error %d (%s) at link %s%s (from %s%s)"LF,r->statuscode,r->msg,urladr,urlfil,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil);
  3535.                 test_flush;
  3536.               }
  3537.               // redemander fichier
  3538.               liens_record(urladr,urlfil,savename,"","");
  3539.               if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3540.                 liens[lien_tot]->testmode=liens[ptr]->testmode;          // mode test?
  3541.                 liens[lien_tot]->link_import=0;       // pas mode import
  3542.                 liens[lien_tot]->depth=liens[ptr]->depth;
  3543.                 liens[lien_tot]->pass2=max(liens[ptr]->pass2,numero_passe);
  3544.                 liens[lien_tot]->retry=liens[ptr]->retry-1;    // moins 1 retry!
  3545.                 liens[lien_tot]->premier=liens[ptr]->premier;
  3546.                 liens[lien_tot]->precedent=liens[ptr]->precedent;
  3547.                 lien_tot++;
  3548.               } else {  // oups erreur, plus de mΘmoire!!
  3549.                 printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3550.                 if (opt->errlog) {
  3551.                   fspc(opt->errlog,"panic"); 
  3552.                   fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3553.                   test_flush;
  3554.                 }
  3555.                 //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3556.                 XH_uninit;    // dΘsallocation mΘmoire & buffers
  3557.                 return 0;
  3558.               } 
  3559.             }
  3560.           } else {
  3561.             if (opt->errlog) {
  3562.               if (opt->debug>1) {
  3563.                 fspc(opt->errlog,"info"); 
  3564.                 fprintf(opt->errlog,"Info: no robots.txt at %s%s"LF,urladr,urlfil);
  3565.               }
  3566.             }
  3567.           }
  3568.           if (!store_errpage) {
  3569.             if (r->adr) {     // dΘsalloc
  3570.               freet(r->adr); 
  3571.               r->adr=NULL; 
  3572.             }
  3573.             error=1;  // erreur!
  3574.           }
  3575.         }
  3576.         // FIN rattrapage des 301,302,307..
  3577.         // ------------------------------------------------------------
  3578.  
  3579.   }  // if !error
  3580.  
  3581.  
  3582.   /* Apply changes */
  3583.   ENGINE_SAVE_CONTEXT();
  3584.  
  3585.   return 0;
  3586.  
  3587.  
  3588. }
  3589.  
  3590.  
  3591.  
  3592. /*
  3593. Wait for next file and
  3594. check 301, 302, .. statuscodes (moved)
  3595. */
  3596. int hts_mirror_wait_for_next_file(htsmoduleStruct* str, htsmoduleStructExtended* stre) {
  3597.   /* Load engine variables */
  3598.   ENGINE_LOAD_CONTEXT();
  3599.   /* */
  3600.   int b;
  3601.   int n;
  3602.  
  3603. #if BDEBUG==1
  3604.   printf("\nBack test..\n");
  3605. #endif
  3606.  
  3607.   // pause/lock files
  3608.   {
  3609.     int do_pause=0;
  3610.  
  3611.     // user pause lockfile : create hts-paused.lock --> HTTrack will be paused
  3612.     if (fexist(fconcat(opt->path_log,"hts-stop.lock"))) {
  3613.       // remove lockfile
  3614.       remove(fconcat(opt->path_log,"hts-stop.lock"));
  3615.       if (!fexist(fconcat(opt->path_log,"hts-stop.lock"))) {
  3616.         do_pause=1;
  3617.       }
  3618.     }
  3619.  
  3620.     // after receving N bytes, pause
  3621.     if (opt->fragment>0) {
  3622.       if ((HTS_STAT.stat_bytes-stat_fragment) > opt->fragment) {
  3623.         do_pause=1;
  3624.       }
  3625.     }
  3626.  
  3627.     // pause?
  3628.     if (do_pause) {
  3629.       if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3630.         fspc(opt->log,"info"); fprintf(opt->log,"engine: pause requested.."LF);
  3631.       }
  3632.       while (back_nsoc(sback)>0) {                  // attendre fin des transferts
  3633.         back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3634.         Sleep(200);
  3635. #if HTS_ANALYSTE
  3636.         {
  3637.           back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3638.  
  3639.           // Transfer rate
  3640.           engine_stats();
  3641.  
  3642.           // Refresh various stats
  3643.           HTS_STAT.stat_nsocket=back_nsoc(sback);
  3644.           HTS_STAT.stat_errors=fspc(NULL,"error");
  3645.           HTS_STAT.stat_warnings=fspc(NULL,"warning");
  3646.           HTS_STAT.stat_infos=fspc(NULL,"info");
  3647.           HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3648.           HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3649.  
  3650.           b=0;
  3651.           if (!hts_htmlcheck_loop(sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)
  3652.             || !back_checkmirror(opt)) {
  3653.               if (opt->errlog) {
  3654.                 fspc(opt->errlog,"info"); fprintf(opt->errlog,"Exit requested by shell or user"LF);
  3655.                 test_flush;
  3656.               }
  3657.               *stre->exit_xh_=1;  // exit requested
  3658.               XH_uninit;
  3659.               return 0;
  3660.             }
  3661.         }
  3662. #endif
  3663.       }
  3664.       // On dΘsalloue le buffer d'enregistrement des chemins crΘΘe, au cas o∙ pendant la pause
  3665.       // l'utilisateur ferait un rm -r aprΦs avoir effectuΘ un tar
  3666.       // structcheck_init(1);
  3667.       {
  3668.         FILE* fp = fopen(fconcat(opt->path_log,"hts-paused.lock"),"wb");
  3669.         if (fp) {
  3670.           fspc(fp,"info");  // dater
  3671.           fprintf(fp,"Pause"LF"HTTrack is paused after retreiving "LLintP" bytes"LF"Delete this file to continue the mirror->.."LF""LF"",(LLint)HTS_STAT.stat_bytes);
  3672.           fclose(fp);
  3673.         }
  3674.       }
  3675.       stat_fragment=HTS_STAT.stat_bytes;
  3676.       /* Info for wrappers */
  3677.       if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3678.         fspc(opt->log,"info"); fprintf(opt->log,"engine: pause: %s"LF,fconcat(opt->path_log,"hts-paused.lock"));
  3679.       }
  3680. #if HTS_ANALYSTE
  3681.       hts_htmlcheck_pause(fconcat(opt->path_log,"hts-paused.lock"));
  3682. #else
  3683.       while (fexist(fconcat(opt->path_log,"hts-paused.lock"))) {
  3684.         //back_wait(sback,opt,cache,HTS_STAT.stat_timestart);   inutile!! (plus de sockets actives)
  3685.         Sleep(1000);
  3686.       }
  3687. #endif
  3688.     }
  3689.     //
  3690.   }
  3691.   // end of pause/lock files
  3692.  
  3693. #if HTS_ANALYSTE
  3694.   // changement dans les prΘfΘrences
  3695.   /*
  3696.   if (_hts_setopt) {
  3697.   copy_htsopt(_hts_setopt,opt);    // copier au besoin
  3698.   _hts_setopt=NULL;                 // effacer callback
  3699.   }
  3700.   */
  3701.   if (_hts_addurl) {
  3702.     char BIGSTK add_adr[HTS_URLMAXSIZE*2];
  3703.     char BIGSTK add_fil[HTS_URLMAXSIZE*2];
  3704.     while(*_hts_addurl) {
  3705.       char BIGSTK add_url[HTS_URLMAXSIZE*2];
  3706.       add_adr[0]=add_fil[0]=add_url[0]='\0';
  3707.       if (!link_has_authority(*_hts_addurl))
  3708.         strcpybuff(add_url,"http://");          // ajouter http://
  3709.       strcatbuff(add_url,*_hts_addurl);
  3710.       if (ident_url_absolute(add_url,add_adr,add_fil)>=0) {
  3711.         // ----Ajout----
  3712.         // noter NOUVEAU lien
  3713.         char BIGSTK add_sav[HTS_URLMAXSIZE*2];
  3714.         // calculer lien et Θventuellement modifier addresse/fichier
  3715.         if (url_savename(add_adr,add_fil,add_sav,NULL,NULL,NULL,NULL,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL)!=-1) { 
  3716.           if (hash_read(hash,add_sav,"",0,0)<0) {      // n'existe pas dΘja
  3717.             // enregistrer lien (MACRO)
  3718.             liens_record(add_adr,add_fil,add_sav,"","");
  3719.             if (liens[lien_tot]!=NULL) {    // OK, pas d'erreur
  3720.               liens[lien_tot]->testmode=0;          // mode test?
  3721.               liens[lien_tot]->link_import=0;       // mode normal
  3722.               liens[lien_tot]->depth=opt->depth;
  3723.               liens[lien_tot]->pass2=max(0,numero_passe);
  3724.               liens[lien_tot]->retry=opt->retry;
  3725.               liens[lien_tot]->premier=lien_tot;
  3726.               liens[lien_tot]->precedent=lien_tot;
  3727.               lien_tot++;
  3728.               //
  3729.               if ((opt->debug>0) && (opt->log!=NULL)) {
  3730.                 fspc(opt->log,"info"); fprintf(opt->log,"Link added by user: %s%s"LF,add_adr,add_fil); test_flush;
  3731.               }
  3732.               //
  3733.             } else {  // oups erreur, plus de mΘmoire!!
  3734.               printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  3735.               if (opt->errlog) {
  3736.                 fprintf(opt->errlog,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url)));
  3737.                 test_flush;
  3738.               }
  3739.               //if (opt->getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  3740.               XH_uninit;    // dΘsallocation mΘmoire & buffers
  3741.               return 0;
  3742.             }
  3743.           } else {
  3744.             if ( (opt->debug>0) && (opt->errlog!=NULL) ) {
  3745.               fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Existing link %s%s not added after user request"LF,add_adr,add_fil);
  3746.               test_flush;
  3747.             }
  3748.           }
  3749.  
  3750.         }
  3751.       } else {
  3752.         if (opt->errlog) {
  3753.           fspc(opt->errlog,"error");
  3754.           fprintf(opt->errlog,"Error during URL decoding for %s"LF,add_url);
  3755.           test_flush;
  3756.         }
  3757.       }
  3758.       // ----Fin Ajout----
  3759.       _hts_addurl++;                  // suivante
  3760.     }
  3761.     _hts_addurl=NULL;           // libΘrer _hts_addurl
  3762.   }
  3763.   // si une pause a ΘtΘ demandΘe
  3764.   if (_hts_setpause || back_pluggable_sockets_strict(sback, opt) <= 0) {
  3765.     // index du lien actuel
  3766.     int b=back_index(sback,urladr,urlfil,savename);
  3767.     int prev = _hts_in_html_parsing;
  3768.     if (b<0) b=0;    // forcer pour les stats
  3769.     while(_hts_setpause || back_pluggable_sockets_strict(sback, opt) <= 0) {    // on fait la pause..
  3770.       _hts_in_html_parsing = 6;
  3771.       back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3772.  
  3773.       // Transfer rate
  3774.       engine_stats();
  3775.  
  3776.       // Refresh various stats
  3777.       HTS_STAT.stat_nsocket=back_nsoc(sback);
  3778.       HTS_STAT.stat_errors=fspc(NULL,"error");
  3779.       HTS_STAT.stat_warnings=fspc(NULL,"warning");
  3780.       HTS_STAT.stat_infos=fspc(NULL,"info");
  3781.       HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3782.       HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3783.  
  3784.       if (!hts_htmlcheck_loop(sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3785.         if (opt->errlog) {
  3786.           fspc(opt->errlog,"info"); fprintf(opt->errlog,"Exit requested by shell or user"LF);
  3787.           test_flush;
  3788.         }
  3789.         *stre->exit_xh_=1;  // exit requested
  3790.         XH_uninit;
  3791.         return 0;
  3792.       }
  3793.       Sleep(100);  // pause
  3794.     }
  3795.     _hts_in_html_parsing = prev;
  3796.   }
  3797. #endif
  3798.  
  3799.   // si le fichier n'est pas en backing, le mettre..
  3800.   if (!back_exist(sback,urladr,urlfil,savename)) {
  3801. #if BDEBUG==1
  3802.     printf("crash backing: %s%s\n",liens[ptr]->adr,liens[ptr]->fil);
  3803. #endif
  3804.     if (back_add(sback,opt,cache,urladr,urlfil,savename,liens[liens[ptr]->precedent]->adr,liens[liens[ptr]->precedent]->fil,liens[ptr]->testmode,&liens[ptr]->pass2)==-1) {
  3805.       printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  3806. #if BDEBUG==1
  3807.       printf("error while crash adding\n");
  3808. #endif
  3809.       if (opt->errlog) {
  3810.         fspc(opt->errlog,"error"); fprintf(opt->errlog,"Unexpected backing error for %s%s"LF,urladr,urlfil);
  3811.         test_flush;
  3812.       } 
  3813.  
  3814.     }
  3815.   }
  3816.  
  3817. #if BDEBUG==1
  3818.   printf("test number of socks\n");
  3819. #endif
  3820.  
  3821.   // ajouter autant de socket qu'on peut ajouter
  3822.   n=opt->maxsoc-back_nsoc(sback);
  3823. #if BDEBUG==1
  3824.   printf("%d sockets available for backing\n",n);
  3825. #endif
  3826.  
  3827. #if HTS_ANALYSTE
  3828.   if ((n>0) && (!_hts_setpause)) {   // si sockets libre et pas en pause, ajouter
  3829. #else
  3830.   if (n>0) {                         // si sockets libre
  3831. #endif
  3832.     // remplir autant que l'on peut le cache (backing)
  3833.     back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3834.   }
  3835.  
  3836.   // index du lien actuel
  3837.   /*
  3838.   b=back_index(sback,urladr,urlfil,savename);
  3839.  
  3840.   if (b>=0) 
  3841.   */
  3842.   {
  3843.     // ------------------------------------------------------------
  3844.     // attendre que le fichier actuel soit prΩt - BOUCLE D'ATTENTE
  3845.     do {
  3846.  
  3847.       // index du lien actuel
  3848.       b=back_index(sback,urladr,urlfil,savename);
  3849. #if BDEBUG==1
  3850.       printf("back index %d, waiting\n",b);
  3851. #endif
  3852.       // Continue to the loop if link still present
  3853.       if (b<0)
  3854.         break;
  3855.  
  3856.       // Receive data
  3857.       if (back[b].status>0)
  3858.         back_wait(sback,opt,cache,HTS_STAT.stat_timestart);
  3859.  
  3860.       // Continue to the loop if link still present
  3861.       b=back_index(sback,urladr,urlfil,savename);
  3862.       if (b<0)
  3863.         break;
  3864.  
  3865.       // Stop the mirror
  3866.       if (!back_checkmirror(opt)) {
  3867.         *stre->exit_xh_=1;  // exit requested
  3868.         XH_uninit;
  3869.         return 0;
  3870.       }
  3871.  
  3872.       // And fill the backing stack
  3873.       if (back[b].status>0)
  3874.         back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  3875.  
  3876.       // Continue to the loop if link still present
  3877.       b=back_index(sback,urladr,urlfil,savename);
  3878.       if (b<0)
  3879.         break;
  3880.  
  3881.       // autres occupations de HTTrack: statistiques, boucle d'attente, etc.
  3882.       if ((opt->makestat) || (opt->maketrack)) {
  3883.         TStamp l=time_local();
  3884.         if ((int) (l-makestat_time) >= 60) {   
  3885.           if (makestat_fp != NULL) {
  3886.             fspc(makestat_fp,"info");
  3887.             fprintf(makestat_fp,"Rate= %d (/"LLintP") \11NewLinks= %d (/%d)"LF,(int) ((HTS_STAT.HTS_TOTAL_RECV-*stre->makestat_total_)/(l-makestat_time)), (LLint)HTS_STAT.HTS_TOTAL_RECV,(int) lien_tot-*stre->makestat_lnk_,(int) lien_tot);
  3888.             fflush(makestat_fp);
  3889.             *stre->makestat_total_=HTS_STAT.HTS_TOTAL_RECV;
  3890.             *stre->makestat_lnk_=lien_tot;
  3891.           }
  3892.           if (stre->maketrack_fp != NULL) {
  3893.             int i;
  3894.             fspc(stre->maketrack_fp,"info"); fprintf(stre->maketrack_fp,LF);
  3895.             for(i=0;i<back_max;i++) {
  3896.               back_info(sback,i,3,stre->maketrack_fp);
  3897.             }
  3898.             fprintf(stre->maketrack_fp,LF);
  3899.             fflush(stre->maketrack_fp);
  3900.  
  3901.           }
  3902.           makestat_time=l;
  3903.         }
  3904.       }
  3905. #if HTS_ANALYSTE
  3906.       {
  3907.         int i;
  3908.         {
  3909.           char* s=hts_cancel_file("");
  3910.           if (strnotempty(s)) {    // fichier α canceller
  3911.             for(i=0;i<back_max;i++) {
  3912.               if ((back[i].status>0)) {
  3913.                 if (strcmp(back[i].url_sav,s)==0) {  // ok trouvΘ
  3914.                   if (back[i].status != 1000) {
  3915. #if HTS_DEBUG_CLOSESOCK
  3916.                     DEBUG_W("user cancel: deletehttp\n");
  3917. #endif
  3918.                     if (back[i].r.soc!=INVALID_SOCKET) deletehttp(&back[i].r);
  3919.                     back[i].r.soc=INVALID_SOCKET;
  3920.                     back[i].r.statuscode=STATUSCODE_INVALID;
  3921.                     strcpybuff(back[i].r.msg,"Cancelled by User");
  3922.                     back[i].status=0;  // terminΘ
  3923.                     back_set_finished(sback, i);
  3924.                   } else    // cancel ftp.. flag α 1
  3925.                     back[i].stop_ftp = 1;
  3926.                 }
  3927.               }
  3928.             }
  3929.             s[0]='\0';
  3930.           }
  3931.         }
  3932.  
  3933.         // Transfer rate
  3934.         engine_stats();
  3935.  
  3936.         // Refresh various stats
  3937.         HTS_STAT.stat_nsocket=back_nsoc(sback);
  3938.         HTS_STAT.stat_errors=fspc(NULL,"error");
  3939.         HTS_STAT.stat_warnings=fspc(NULL,"warning");
  3940.         HTS_STAT.stat_infos=fspc(NULL,"info");
  3941.         HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  3942.         HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  3943.  
  3944.         if (!hts_htmlcheck_loop(sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  3945.           if (opt->errlog) {
  3946.             fspc(opt->errlog,"info"); fprintf(opt->errlog,"Exit requested by shell or user"LF);
  3947.             test_flush;
  3948.           } 
  3949.           *stre->exit_xh_=1;  // exit requested
  3950.           XH_uninit;
  3951.           return 0;
  3952.         }
  3953.       }
  3954.  
  3955. #endif
  3956. #if HTS_POLL
  3957.       if ((opt->shell) || (opt->keyboard) || (opt->verbosedisplay) || (!opt->quiet)) {
  3958.         TStamp tl;
  3959.         *stre->info_shell_=1;
  3960.  
  3961.         /* Toggle with ENTER */
  3962.         if (!opt->quiet) {
  3963.           if (check_stdin()) {
  3964.             char com[256];
  3965.             linput(stdin,com,200);
  3966.             if (opt->verbosedisplay==2)
  3967.               opt->verbosedisplay=1;
  3968.             else
  3969.               opt->verbosedisplay=2;
  3970.             /* Info for wrappers */
  3971.             if ( (opt->debug>0) && (opt->log!=NULL) ) {
  3972.               fspc(opt->log,"info"); fprintf(opt->log,"engine: change-options"LF);
  3973.             }
  3974. #if HTS_ANALYSTE
  3975.             hts_htmlcheck_chopt(opt);
  3976. #endif
  3977.           }
  3978.         }
  3979.  
  3980.         tl=time_local();
  3981.  
  3982.         // gΘnΘrer un message d'infos sur l'Θtat actuel
  3983.         if (opt->shell) {    // si shell
  3984.           if ((tl-*stre->last_info_shell_)>0) {    // toute les 1 sec
  3985.             FILE* fp=stdout;
  3986.             int a=0;
  3987.             *stre->last_info_shell_=tl;
  3988.             if (fexist(fconcat(opt->path_log,"hts-autopsy"))) {  // dΘbuggage: teste si le robot est vivant
  3989.               // (oui je sais un robot vivant.. mais bon.. il a le droit de vivre lui aussi)
  3990.               // (libΘrons les robots esclaves de l'internet!)
  3991.               remove(fconcat(opt->path_log,"hts-autopsy"));
  3992.               fp=fopen(fconcat(opt->path_log,"hts-isalive"),"wb");
  3993.               a=1;
  3994.             }
  3995.             if ((*stre->info_shell_) || a) {
  3996.               int i,j;
  3997.  
  3998.               fprintf(fp,"TIME %d"LF,(int) (tl-HTS_STAT.stat_timestart));
  3999.               fprintf(fp,"TOTAL %d"LF,(int) HTS_STAT.stat_bytes);
  4000.               fprintf(fp,"RATE %d"LF,(int) (HTS_STAT.HTS_TOTAL_RECV/(tl-HTS_STAT.stat_timestart)));
  4001.               fprintf(fp,"SOCKET %d"LF,back_nsoc(sback));
  4002.               fprintf(fp,"LINK %d"LF,lien_tot);
  4003.               {
  4004.                 LLint mem=0;
  4005.                 for(i=0;i<back_max;i++)
  4006.                   if (back[i].r.adr!=NULL)
  4007.                     mem+=back[i].r.size;
  4008.                 fprintf(fp,"INMEM "LLintP""LF,(LLint)mem);
  4009.               }
  4010.               for(j=0;j<2;j++) {  // passes pour ready et wait
  4011.                 for(i=0;i<back_max;i++) {
  4012.                   back_info(sback,i,j+1,stdout);    // maketrack_fp a la place de stdout ?? // **
  4013.                 }
  4014.               }
  4015.               fprintf(fp,LF);
  4016.               if (a)
  4017.                 fclose(fp);
  4018.               io_flush;
  4019.             }
  4020.           }
  4021.         }  // si shell
  4022.  
  4023.       }  // si shell ou keyboard (option)
  4024.       //
  4025. #endif
  4026.     } while((b>=0) && (back[max(b,0)].status>0));
  4027.  
  4028.  
  4029.     // If link not found on the stack, it's because it has already been downloaded
  4030.     // in background
  4031.     // Then, skip it and go to the next one
  4032.     if (b<0) {
  4033.       if ((opt->debug>1) && (opt->log!=NULL)) {
  4034.         fspc(opt->log,"debug"); fprintf(opt->log,"link #%d is ready, no more on the stack, skipping: %s%s.."LF,ptr,urladr,urlfil);
  4035.         test_flush;
  4036.       }
  4037.  
  4038.       // prochain lien
  4039.       // ptr++;
  4040.  
  4041.       return 2; // goto jump_if_done;
  4042.  
  4043.     }
  4044. #if 0
  4045.     /* FIXME - finalized HAS NO MORE THIS MEANING */
  4046.     /* link put in cache by the backing system for memory spare - reclaim */
  4047.     else if (back[b].finalized) {
  4048.       assertf(back[b].r.adr == NULL);
  4049.       /* read file in cache */
  4050.       back[b].r = cache_read_ro(opt,cache,back[b].url_adr,back[b].url_fil,back[b].url_sav, back[b].location_buffer);
  4051.       /* ensure correct location buffer set */
  4052.       back[b].r.location=back[b].location_buffer;
  4053.       if (back[b].r.statuscode == STATUSCODE_INVALID) {
  4054.         if (opt->errlog) {
  4055.           fspc(opt->errlog,"error"); fprintf(opt->errlog,"Unexpected error: %s%s not found anymore in cache"LF,back[b].url_adr,back[b].url_fil);
  4056.           test_flush;
  4057.         }
  4058.       } else {
  4059.         if ( (opt->debug>1) && (opt->log!=NULL) ) {
  4060.           fspc(opt->log,"debug"); fprintf(opt->log,"reclaim file %s%s (%d)"LF,back[b].url_adr,back[b].url_fil,back[b].r.statuscode); test_flush;
  4061.         }
  4062.       }
  4063.     }
  4064. #endif
  4065.  
  4066. #if HTS_ANALYSTE==2
  4067. #else
  4068.     //if (!opt->quiet) {  // petite animation
  4069.     if (!opt->verbosedisplay) {
  4070.       if (!opt->quiet) {
  4071.         static int roll=0;  /* static: ok */
  4072.         roll=(roll+1)%4;
  4073.         printf("%c\x0d",("/-\\|")[roll]);
  4074.         fflush(stdout);
  4075.       }
  4076.     } else if (opt->verbosedisplay==1) {
  4077.       if (b >= 0) {
  4078.         if (back[b].r.statuscode==200)
  4079.           printf("%d/%d: %s%s ("LLintP" bytes) - OK\33[K\r",ptr,lien_tot,back[b].url_adr,back[b].url_fil,(LLint)back[b].r.size);
  4080.         else
  4081.           printf("%d/%d: %s%s ("LLintP" bytes) - %d\33[K\r",ptr,lien_tot,back[b].url_adr,back[b].url_fil,(LLint)back[b].r.size,back[b].r.statuscode);
  4082.       } else {
  4083.         fspc(opt->errlog,"error"); fprintf(opt->errlog,"Link disappeared");
  4084.       }
  4085.       fflush(stdout);
  4086.     }
  4087.     //}
  4088. #endif
  4089.     // ------------------------------------------------------------
  4090.     // VΘrificateur d'intΘgritΘ
  4091. #if DEBUG_CHECKINT
  4092.     _CHECKINT(&back[b],"Retour de back_wait, aprΦs le while")
  4093.     {
  4094.       int i;
  4095.       for(i=0;i<back_max;i++) {
  4096.         char si[256];
  4097.         sprintf(si,"Test global aprΦs back_wait, index %d",i);
  4098.         _CHECKINT(&back[i],si)
  4099.       }
  4100.     }
  4101. #endif
  4102.  
  4103.     // copier structure rΘponse htsblk
  4104.     if (b >= 0) {
  4105.       memcpy(r, &(back[b].r), sizeof(htsblk));
  4106.       r->location=stre->loc_;    // ne PAS copier location!! adresse, pas de buffer
  4107.       if (back[b].r.location) 
  4108.         strcpybuff(r->location,back[b].r.location);
  4109.       back[b].r.adr=NULL;    // ne pas faire de desalloc ensuite
  4110.  
  4111.       // libΘrer emplacement backing
  4112.       back_maydelete(opt,cache,sback,b);
  4113.     }
  4114.  
  4115.     // progression
  4116. #if 0
  4117.     if (opt->aff_progress) {
  4118.       TStamp tl=time_local();
  4119.       if ((tl-HTS_STAT.stat_timestart)>0) {
  4120.         char s[32];
  4121.         int i=0;
  4122.         lastime=tl;
  4123.         _CLRSCR; _GOTOXY("1","1");
  4124.         printf("Rate=%d B/sec\n",(int) (HTS_STAT.HTS_TOTAL_RECV/(tl-HTS_STAT.stat_timestart)));
  4125.         while(i<minimum(back_max,99)) {  // **
  4126.           if (back[i].status>=0) {  // loading..
  4127.             s[0]='\0';
  4128.             if (strlen(back[i].url_fil)>16)
  4129.               strcatbuff(s,back[i].url_fil+strlen(back[i].url_fil)-16);       
  4130.             else
  4131.               strncatbuff(s,back[i].url_fil,16);
  4132.             printf("%s : ",s);
  4133.  
  4134.             printf("[");
  4135.             if (back[i].r.totalsize>0) {
  4136.               int p;
  4137.               int j;
  4138.               p=(int)((back[i].r.size*10)/back[i].r.totalsize);
  4139.               p=minimum(10,p);
  4140.               for(j=0;j<p;j++) printf("*");
  4141.               for(j=0;j<(10-p);j++) printf("-");
  4142.             } else { 
  4143.               printf(LLintP,(LLint)back[i].r.size);                      
  4144.             }
  4145.             printf("]");
  4146.  
  4147.             //} else if (back[i].status==0) {
  4148.             //  strcpybuff(s,"ENDED");
  4149.           } 
  4150.           printf("\n");
  4151.           i++;
  4152.         }
  4153.         io_flush;
  4154.       }
  4155.     }
  4156. #endif
  4157.  
  4158.     // dΘbug graphique
  4159. #if BDEBUG==2
  4160.     {
  4161.       char s[12];
  4162.       int i=0;
  4163.       _GOTOXY(1,1);
  4164.       printf("Rate=%d B/sec\n",(int) (HTS_STAT.HTS_TOTAL_RECV/(time_local()-HTS_STAT.stat_timestart)));
  4165.       while(i<minimum(back_max,160)) {
  4166.         if (back[i].status>0) {
  4167.           sprintf(s,"%d",back[i].r.size);
  4168.         } else if (back[i].status==0) {
  4169.           strcpybuff(s,"ENDED");
  4170.         } else 
  4171.           strcpybuff(s,"   -   ");
  4172.         while(strlen(s)<8) strcatbuff(s," ");
  4173.         printf("%s",s); io_flush;
  4174.         i++;
  4175.       }
  4176.     }
  4177. #endif
  4178.  
  4179.  
  4180. #if BDEBUG==1
  4181.     printf("statuscode=%d with %s / msg=%s\n",r->statuscode,r->contenttype,r->msg);
  4182. #endif
  4183.  
  4184.   }
  4185.   /*else {
  4186.   #if BDEBUG==1
  4187.   printf("back index error\n");
  4188.   #endif
  4189.   }
  4190.   */
  4191.  
  4192.   ENGINE_SAVE_CONTEXT();
  4193.   return 0;
  4194. }
  4195.  
  4196. /* Wait for delayed types */
  4197. int hts_wait_delayed(htsmoduleStruct* str, 
  4198.                      char* adr, char* fil, char* save, 
  4199.                      char* former_adr, char* former_fil, 
  4200.                      int* forbidden_url) {
  4201.   ENGINE_LOAD_CONTEXT_BASE();
  4202.   hash_struct* hash = hashptr;
  4203.  
  4204.   int r_sv=0;
  4205.  
  4206.   // resolve unresolved type
  4207.   if (opt->savename_delayed != 0
  4208.     && *forbidden_url == 0 
  4209.     && IS_DELAYED_EXT(save) 
  4210.     && !opt->state.stop
  4211.     )
  4212.   {
  4213.     int loops=0;
  4214.     int continue_loop = 1;
  4215.     if ((opt->debug>1) && (opt->log!=NULL)) {
  4216.       fspc(opt->log,"debug"); fprintf(opt->log,"Waiting for type to be known: %s%s"LF, adr, fil);
  4217.       test_flush;
  4218.     }
  4219.  
  4220.     /* Follow while type is unknown and redirects occurs */
  4221.     while(IS_DELAYED_EXT(save) && continue_loop && loops++ < 7) {
  4222.       continue_loop = 0;
  4223.  
  4224.       /*
  4225.       Wait for an available slot 
  4226.       */
  4227.       WAIT_FOR_AVAILABLE_SOCKET();
  4228.  
  4229.       /* We can lookup directly in the cache to speedup this mess */
  4230.       if (opt->delayed_cached) {
  4231.                 lien_back back;
  4232.                 memset(&back, 0, sizeof(back));
  4233.                 back.r = cache_read(opt, cache, adr, fil, NULL, NULL);              // test uniquement
  4234.         if (back.r.statuscode == 200 && strnotempty(back.r.contenttype)) {      // cache found, and aswer is 'OK'
  4235.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4236.             fspc(opt->log,"debug"); fprintf(opt->log,"Direct type lookup in cache (-%%D1): %s"LF, back.r.contenttype);
  4237.             test_flush;
  4238.           }
  4239.  
  4240.           /* Recompute filename with MIME type */
  4241.           save[0] = '\0';
  4242.           r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&back);
  4243.  
  4244.           /* Recompute authorization with MIME type */
  4245.           {
  4246.             int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, back.r.contenttype);
  4247.             if (new_forbidden_url != -1) {
  4248.               if ((opt->debug>1) && (opt->log!=NULL)) {
  4249.                 fspc(opt->log,"debug"); fprintf(opt->log,"result for wizard mime test: %s"LF,new_forbidden_url);
  4250.                 test_flush;
  4251.               }
  4252.               if (new_forbidden_url == 1) {
  4253.                 *forbidden_url = new_forbidden_url;
  4254.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4255.                   fspc(opt->log,"debug"); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4256.                   test_flush;
  4257.                 }
  4258.                 break;        // exit loop
  4259.               }
  4260.             }
  4261.           }
  4262.  
  4263.           /* And exit loop */
  4264.           break;
  4265.         }
  4266.       }
  4267.  
  4268.       /* Add in backing (back_index() will respond correctly) */
  4269.       if (back_add_if_not_exists(sback,opt,cache,adr,fil,save,NULL,NULL,0,NULL) != -1) {
  4270.         int b;
  4271.         b=back_index(sback,adr,fil,save); 
  4272.         if (b<0) {
  4273.           printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4274.           XH_uninit;    // dΘsallocation mΘmoire & buffers
  4275.           return -1;
  4276.         }
  4277.  
  4278.         /* Cache read failed because file does not exists (bad delayed name!)
  4279.         Just re-add with the correct name, as we know the MIME now!
  4280.         */
  4281.         if (back[b].r.statuscode == STATUSCODE_INVALID && back[b].r.adr == NULL) {
  4282.                     lien_back delayed_back;
  4283.           //char BIGSTK delayed_ctype[128];
  4284.           // delayed_ctype[0] = '\0';
  4285.           // strncatbuff(delayed_ctype, back[b].r.contenttype, sizeof(delayed_ctype) - 1);    // copier content-type
  4286.                     back_copy_static(&back[b], &delayed_back);
  4287.  
  4288.           /* Delete entry */
  4289.           back_delete(opt,cache,sback,b);       // cancel
  4290.           b = -1;
  4291.  
  4292.           /* Recompute filename with MIME type */
  4293.           save[0] = '\0';
  4294.           r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&delayed_back);
  4295.  
  4296.           /* Recompute authorization with MIME type */
  4297.           {
  4298.             int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, delayed_back.r.contenttype);
  4299.             if (new_forbidden_url != -1) {
  4300.               if ((opt->debug>1) && (opt->log!=NULL)) {
  4301.                 fspc(opt->log,"debug"); fprintf(opt->log,"result for wizard mime test: %d"LF,*forbidden_url);
  4302.                 test_flush;
  4303.               }
  4304.               if (new_forbidden_url == 1) {
  4305.                 *forbidden_url = new_forbidden_url;
  4306.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4307.                   fspc(opt->log,"debug"); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4308.                   test_flush;
  4309.                 }
  4310.                 break;        // exit loop
  4311.               }
  4312.             }
  4313.           }
  4314.  
  4315.           /* Re-Add wiht correct type */
  4316.           if (back_add_if_not_exists(sback,opt,cache,adr,fil,save,NULL,NULL,0,NULL) != -1) {
  4317.             b=back_index(sback,adr,fil,save); 
  4318.           }
  4319.           if (b<0) {
  4320.             printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4321.             XH_uninit;    // dΘsallocation mΘmoire & buffers
  4322.             return -1;
  4323.           }
  4324.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4325.             fspc(opt->log,"debug"); fprintf(opt->log,"Type immediately loaded from cache: %s"LF, delayed_back.r.contenttype);
  4326.             test_flush;
  4327.           }
  4328.         }
  4329.  
  4330.         /* Wait for headers to be received */
  4331.         do {
  4332.           if (b < 0)
  4333.             break;
  4334.  
  4335.           // temps α attendre, et remplir autant que l'on peut le cache (backing)
  4336.           if (back[b].status>0) {
  4337.             back_wait(sback,opt,cache,0);
  4338.           }
  4339.           if (ptr>=0) {
  4340.             back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  4341.           }
  4342.  
  4343.           // on est obligΘ d'appeler le shell pour le refresh..
  4344. #if HTS_ANALYSTE
  4345.           {
  4346.  
  4347.             // Transfer rate
  4348.             engine_stats();
  4349.  
  4350.             // Refresh various stats
  4351.             HTS_STAT.stat_nsocket=back_nsoc(sback);
  4352.             HTS_STAT.stat_errors=fspc(NULL,"error");
  4353.             HTS_STAT.stat_warnings=fspc(NULL,"warning");
  4354.             HTS_STAT.stat_infos=fspc(NULL,"info");
  4355.             HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  4356.             HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  4357.  
  4358.             if (!hts_htmlcheck_loop(sback->lnk, sback->count, b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  4359.               return -1;
  4360.             } else if (_hts_cancel || !back_checkmirror(opt)) {    // cancel 2 ou 1 (cancel parsing)
  4361.               back_delete(opt,cache,sback,b);       // cancel test
  4362.               break;
  4363.             }
  4364.           }
  4365. #endif
  4366.         } while(
  4367.           /* dns/connect/request */ 
  4368.                     ( back[b].status >= 99 && back[b].status <= 101 )
  4369.           ||
  4370.           /* For redirects, wait for request to be terminated */
  4371.           ( HTTP_IS_REDIRECT(back[b].r.statuscode) && back[b].status > 0 )
  4372.           ||
  4373.           /* Same for errors */
  4374.           ( HTTP_IS_ERROR(back[b].r.statuscode) && back[b].status > 0 )
  4375.           );
  4376.         /* ready (chunked) or ready (regular download) or ready (completed) */
  4377.  
  4378.         // Note: filename NOT in hashtable yet - liens_record will do it, with the correct ext!
  4379.         if (b >= 0) {
  4380.                     lien_back delayed_back;
  4381.           //char BIGSTK delayed_ctype[128];
  4382.           //delayed_ctype[0] = '\0';
  4383.           //strncatbuff(delayed_ctype, back[b].r.contenttype, sizeof(delayed_ctype) - 1);    // copier content-type
  4384.                     back_copy_static(&back[b], &delayed_back);
  4385.  
  4386.           /* Error */
  4387.           if (HTTP_IS_ERROR(back[b].r.statuscode))
  4388.           {
  4389.             /* 'no error page' selected or file discarded by size rules! */
  4390.             if (!opt->errpage || ( back[b].r.statuscode == STATUSCODE_TOO_BIG ) ) {
  4391.               /* Note: the cache 'cached_tests' system will remember this error, and we'll only issue ONE request */
  4392.               *forbidden_url = 1;          /* Forbidden! */
  4393.               if (opt->log != NULL) {
  4394.                 if (back[b].r.statuscode == STATUSCODE_TOO_BIG) {
  4395.                   fspc(opt->log,"error"); fprintf(opt->log,"link not taken because of its size (%d bytes) at %s%s"LF,(int)back[b].r.totalsize,adr,fil);
  4396.                 } else {
  4397.                   fspc(opt->log,"error"); fprintf(opt->log,"link not taken because of error (%d '%s') at %s%s"LF,back[b].r.statuscode,back[b].r.msg,adr,fil);
  4398.                 }
  4399.                 test_flush;
  4400.               }
  4401.               break;
  4402.             }
  4403.           }
  4404.           /* Moved! */
  4405.           else if (HTTP_IS_REDIRECT(back[b].r.statuscode))
  4406.           {
  4407.             char BIGSTK mov_url[HTS_URLMAXSIZE*2];
  4408.             mov_url[0] = '\0';
  4409.             strcpybuff(mov_url, back[b].r.location);    // copier URL
  4410.  
  4411.             /* Remove (temporarily created) file if it was created */
  4412.             unlink(fconv(back[b].url_sav));
  4413.  
  4414.             /* Remove slot! */
  4415.             if (back[b].status == 0) {
  4416.               back_maydelete(opt, cache, sback, b);
  4417.             } else {    /* should not happend */
  4418.               back_delete(opt, cache, sback, b);
  4419.             }
  4420.             b = -1;
  4421.  
  4422.             /* Handle redirect */
  4423.             if ((int) strnotempty(mov_url)) {    // location existe!
  4424.               char BIGSTK mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  4425.               mov_adr[0]=mov_fil[0]='\0';
  4426.               //
  4427.               if (ident_url_relatif(mov_url,adr,fil,mov_adr,mov_fil)>=0) {                        
  4428.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4429.                   fspc(opt->log,"debug"); fprintf(opt->log,"Redirect while resolving type: %s%s -> %s%s"LF, adr, fil, mov_adr, mov_fil);
  4430.                   test_flush;
  4431.                 }
  4432.                 // si non bouclage sur soi mΩme, ou si test avec GET non testΘ
  4433.                 if (strcmp(mov_adr,adr) != 0 || strcmp(mov_fil,fil) != 0) {
  4434.  
  4435.                   // recopier former_adr/fil?
  4436.                   if ((former_adr) && (former_fil)) {
  4437.                     if (strnotempty(former_adr)==0) {    // Pas dΘja notΘ
  4438.                       strcpybuff(former_adr,adr);
  4439.                       strcpybuff(former_fil,fil);
  4440.                     }
  4441.                   }
  4442.  
  4443.                   // check explicit forbidden - don't follow 3xx in this case
  4444.                   {
  4445.                     int set_prio_to=0;
  4446.                     robots_wizard* robots = (robots_wizard*) opt->robotsptr;
  4447.                     if (hts_acceptlink(opt,ptr,lien_tot,liens,
  4448.                       mov_adr,mov_fil,
  4449.                       NULL, NULL,
  4450.                       &set_prio_to,
  4451.                       NULL) == 1) 
  4452.                     {  /* forbidden */
  4453.                       /* Note: the cache 'cached_tests' system will remember this error, and we'll only issue ONE request */
  4454.                       *forbidden_url = 1;          /* Forbidden! */
  4455.                       if ((opt->debug>1) && (opt->log!=NULL)) {
  4456.                         fspc(opt->log,"debug"); fprintf(opt->log,"link forbidden because of redirect beyond the mirror scope at %s%s"LF,adr,fil);
  4457.                         test_flush;
  4458.                       }
  4459.                       strcpybuff(adr,mov_adr);
  4460.                       strcpybuff(fil,mov_fil);
  4461.                       mov_url[0]='\0';
  4462.                       break;
  4463.                     }
  4464.                   }
  4465.  
  4466.                   // ftp: stop!
  4467.                   if (strfield(mov_url,"ftp://")
  4468. #if HTS_USEMMS
  4469.                                         || strfield(mov_url,"mms://")
  4470. #endif
  4471.                                         ) {
  4472.                     strcpybuff(adr,mov_adr);
  4473.                     strcpybuff(fil,mov_fil);
  4474.                     break;
  4475.                   }
  4476.  
  4477.                   /* ok, continue */
  4478.                   strcpybuff(adr,mov_adr);
  4479.                   strcpybuff(fil,mov_fil);
  4480.                   continue_loop = 1;
  4481.                 } else {
  4482.                   if ( opt->errlog!=NULL ) {
  4483.                     fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Unable to test %s%s (loop to same filename)"LF,adr,fil);
  4484.                     test_flush;
  4485.                   }
  4486.                 }  // loop to same location
  4487.               }  // ident_url_relatif()
  4488.             }  // location
  4489.           }  // redirect
  4490.           if ((opt->debug>1) && (opt->log!=NULL)) {
  4491.                         fspc(opt->log,"debug"); fprintf(opt->log,"Final type for %s%s: '%s'"LF, adr, fil, delayed_back.r.contenttype);
  4492.             test_flush;
  4493.           }
  4494.  
  4495.           /* Recompute filename with MIME type */
  4496.           save[0] = '\0';
  4497.           r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,&delayed_back);
  4498.  
  4499.           /* Recompute authorization with MIME type */
  4500.           {
  4501.                         int new_forbidden_url = hts_acceptmime(opt, ptr, lien_tot, liens, adr,fil, delayed_back.r.contenttype);
  4502.             if (new_forbidden_url != -1) {
  4503.               if ((opt->debug>1) && (opt->log!=NULL)) {
  4504.                 fspc(opt->log,"debug"); fprintf(opt->log,"result for wizard mime test: %d"LF,forbidden_url);
  4505.                 test_flush;
  4506.               }
  4507.               if (new_forbidden_url == 1) {
  4508.                 *forbidden_url = new_forbidden_url;
  4509.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  4510.                   fspc(opt->log,"debug"); fprintf(opt->log,"link forbidden because of MIME types restrictions: %s%s"LF, adr, fil);
  4511.                   test_flush;
  4512.                 }
  4513.                 break;        // exit loop
  4514.               }
  4515.             }
  4516.           }
  4517.  
  4518.           /* Still have a back reference */
  4519.           if (b >= 0) {
  4520.             /* Finalize now as we have the type */
  4521.                         if (back[b].status == 0) {
  4522.                             if (!back[b].finalized) {
  4523.                                 back_finalize(opt,cache,sback,b);
  4524.                             }
  4525.                         }
  4526.             /* Patch destination filename for direct-to-disk mode */
  4527.             strcpybuff(back[b].url_sav, save);
  4528.           }
  4529.  
  4530.         }  // b >= 0
  4531.       } else {
  4532.         printf("PANIC! : Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  4533.         XH_uninit;    // dΘsallocation mΘmoire & buffers
  4534.         return -1;
  4535.       }
  4536.  
  4537.     } // while(IS_DELAYED_EXT(save))
  4538.  
  4539.     // error
  4540.     if (*forbidden_url != 1
  4541.       && IS_DELAYED_EXT(save)) {
  4542.       *forbidden_url = 1;
  4543.       if (opt->log!=NULL) {
  4544.         fspc(opt->log,"warning"); fprintf(opt->log,"link is probably looping, type unknown, aborting: %s%s"LF, adr, fil);
  4545.         test_flush;
  4546.       }
  4547.     }
  4548.  
  4549.   }  // delayed type check ?
  4550.  
  4551.   ENGINE_SAVE_CONTEXT_BASE();
  4552.  
  4553.   return 0;
  4554. }
  4555.  
  4556.